Huỳ*_*yễn 9 javascript reactjs vue.js
在 React 中,我可以像这样解构 props:
function MyComponent() {
const myProp = {
cx: '50%',
cy: '50%',
r: '45%',
'stroke-width': '10%'
}
return (
<svg>
<circle {...myProp}> </circle>
</svg>
)
}
Run Code Online (Sandbox Code Playgroud)
我如何在 Vue 中做同样的事情?我目前在 VueJS 中的详细版本是这样的:
<svg>
<circle :cx="circle.cx" :cy="circle.cy" :r="circle.r" :stroke-width="circle.strokeWidth"> </circle>
</svg>
computed: {
circle() {
return {
cx: '50%',
cy: '50%',
r: '45%',
strokeWidth: '10%'
}
}
}
Run Code Online (Sandbox Code Playgroud)
在可运行的代码片断阵营:https://jsfiddle.net/as0p2hgw/
在Vue公司可运行的代码片段:https://jsfiddle.net/zsd42uve/
只是添加到 CMS 答案,因为这对于给定的示例不起作用(完全),因为“笔划宽度”未正确传递(笔划宽度需要为 kebab-case)。为了使其发挥作用,需要:
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<my-component></my-component>
</div>
<script>
Vue.component('my-component', {
template: `
<svg>
<circle v-bind="circle"> </circle>
</svg>
`,
computed: {
circle() {
return {
cx: '50%',
cy: '50%',
r: '45%',
'stroke-width': '10%'
}
}
}
})
new Vue({
el: '#app'
})
</script>
Run Code Online (Sandbox Code Playgroud)
您可以使用该v-bind指令将对象的所有属性绑定为 props:
<svg>
<circle v-bind="circle"> </circle>
</svg>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2428 次 |
| 最近记录: |