<div id="app">
<!-- <h2>{{firstName}}{{lastName}}</h2> -->
<h2>{{fullName}}</h2>
</div>
<script type="text/javascript">
const app = new Vue({
el: ‘#app‘,
data: {
firstName: ‘Kosfe‘,
lastName: ‘Ccnkf‘
},
computed: {
fullName() {
return this.firstName + this.lastName
}
}
})
</script>
// 使用计算属性计算出书的总价格
<div id="app">
<h3>总价格:{{totalPrice}}</h3>
<script type="text/javascript">
const app = new Vue({
el: ‘#app‘,
data: {
books:[
{name:‘三国演义‘, price:29},
{name:‘红楼梦‘, price:22},
{name:‘西游记‘, price:27},
]
},
computed: {
totalPrice: function(){
let result = 0;
for (let book of this.books) {
result += book.price;
}
return result;
}
}
})
</script>
<script type="text/javascript">
const app = new Vue({
computed: {
fullName: {
set: function(newValue) {
console.log(‘-----‘, newValue);
const names = newValue.split(‘b‘);
this.firstName = names[0];
this.lastName = names[1];
},
get: function() {
return this.firstName + ‘ ‘ + this.lastName
}
}
}
})
</script>
原文:https://www.cnblogs.com/jincanyu/p/14352294.html