标签:ant ast 返回值 使用 price code 求值 执行 foreach
<div id="app">
<h2>{{firstName}} {{lastName}}</h2>
<h2>{{fullName}}</h2>
</div>
<script src="../../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
firstName: 'Kobe',
lastName: 'Bryant'
},
computed: {
fullName() {
return this.firstName + ' ' + this.lastName
}
}
<div id="app">
<h2>总价值: {{totalPrice}}</h2>
</div>
<script src="../../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
books: [
{name: 'Java编程思想', price: 99, count: 3},
{name: 'Unix编程艺术', price: 118, count: 2},
{name: 'Vuejs程序设计', price: 89, count: 1},
]
},
computed: {
totalPrice() {
// 1.第一种方式
/*
let total = 0
this.books.forEach(book => {
total += book.price * book.count
})
return total
*/
// 2.第二种方式
// let total = 0
// for (let i in this.books) {
// const book = this.books[i]
// total += book.price * book.count
// }
// return total
// 3.第三种方式,高阶函数
return this.books.reduce((preValue, book) => {
return preValue + book.price * book.count
}, 0)
}
}
})
<div id="app">
<h2>{{fullName}}</h2>
</div>
<script src="../../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
firstName: 'Kobe',
lastName: 'Bryant'
},
computed: {
fullName: {
set(newValue) {
const names = newValue.split(' ')
this.firstName = names[0]
this.lastName = names[1]
},
get() {
return this.firstName + ' ' + this.lastName
}
}
}
})
computed: {
fullName() {
console.log('调用了computed中的fullName');
return this.firstName + ' ' + this.lastName
}
},
methods: {
getFullName() {
console.log('调用了methods中的getFullName');
return this.firstName + ' ' + this.lastName
}
}
标签:ant ast 返回值 使用 price code 求值 执行 foreach
原文地址:https://www.cnblogs.com/landuo629/p/12409202.html