(2)使用计算属性
<!DOCTYPE html>
<html lang="zh"> <head>
<meta charset="UTF-8" />
<title>Vue</title>
</head>//前端全栈交流学习圈:
//866109386,帮助1-3年前端人员
<body>//突破技术瓶颈,提升思维能力
<div id="app">
<my-component :width='100'></my-component>
</div>
<script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script>
<script type="text/javascript">
Vue.component('my-component', {
template: '<div :style="style">组件内容</div>',
props: ['width'],
computed: {
style: function() {
return {
width: this.width + 'px'
}
}
}
})
new Vue({
el: "#app"
})
</script>
</body>
</html>










