首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过viewBox计算SVG viewBox?

如何通过viewBox计算SVG viewBox?
EN

Stack Overflow用户
提问于 2019-05-25 22:18:17
回答 2查看 1.6K关注 0票数 0

如何通过vue.js自动评估SVG的视图框?

我的代码:

代码语言:javascript
复制
Vue.component('icon', {
props: {
    width: {
        type: Number,
        default: 24
    },
    height: {
        type: Number,
        default: 24
    },
    viewBox: {
        default: 0 0 + width + height
    }
},
template: '<svg xmlns="http://www.w3.org/2000/svg" :viewBox="viewBox" :width="width" :height="height"></svg>',
})

0 +宽度+高度不工作,如何修复它?我是新来的Vue.js。

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-25 22:35:05

除了应该将viewBox定义为@ittus指出的计算值之外,0 0 + width + height不计算字符串,因为:

0 0是无效的javascript (Uncaught SyntaxError: Unexpected number)

( b) widthheight都是数字。0 + width + height将计算为一个数字,默认为48。

使用以下之一创建字符串:

级联(在这里读更多):

代码语言:javascript
复制
viewBox: {
    default: '0 0 ' + this.width + ' ' + this.height
}

或模板文字(在这里读更多):

代码语言:javascript
复制
viewBox: {
    default: `0 0 ${this.width} ${this.height}`
}
票数 3
EN

Stack Overflow用户

发布于 2019-05-25 22:25:31

你应该用computed代替

代码语言:javascript
复制
Vue.component('icon', {
props: {
    width: {
        type: Number,
        default: 24
    },
    height: {
        type: Number,
        default: 24
    }
},
computed: {
    viewBox() {
        return '0 0 ' + this.width + ' ' + this.height
    }
}
template: '<svg xmlns="http://www.w3.org/2000/svg" :viewBox="viewBox" :width="width" :height="height"></svg>',
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56309164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档