通常,在模板中可直接通过插值语法显示data中的属性,但是在某些情况,需要将某些数据进行转化后显示或者将多个数据结合起来显示。
<body>
<div id="app">
<h2>{{surname + ' ' + name}}</h2>
<h2>{{surname}} {{name}}</h2>
<h2>{{getFullName()}}</h2>
<h2>{{fullName}}</h2>
</div>
<script src="./js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
surname: '张',
name: '三'
},
methods: {
getFullName: function(){
return this.surname + ' ' + this.name
}
},
computed: {
fullName: function(){
return this.surname + ' ' + this.name
}
}
})
</script>
</body>
效果图:
<body>
<div id="app">
<h2>总价:{{totlePrice}}</h2>
</div>
<script src="./js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
books:[
{id: 101,name: 'Uinx编程技术',price: 108},
{id: 102,name: '代码大全',price: 99},
{id: 103,name: '深入理解计算机原理',price: 70},
{id: 104,name: '现代操作系统',price: 58},
]
},
computed: {
totlePrice: function(){
let result = 0;
for (i = 0; i < this.books.length; i++){
result += this.books[i].price;
}
return result;
}
}
})
</script>
</body>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)