1、computed基本使用
const vm = new Vue({
el: '#app',
data: {
firstName: '二狗',
lastName: '刘',
xx: '1'
},
methods: {
fullName() {
return this.firstName = this.lastName;
}
},
computed: {
fullName() {
return this.firstName + this.lastName;
}
}
})
2、computed的get和set方法
const vm = new Vue({
el: '#app',
data: {
firstName: '二狗',
lastName: '刘',
xx: '1'
},
methods: {
fullName() {
return this.firstName = this.lastName;
}
},
computed: {
fullName() {
return this.firstName + this.lastName;
}
}
})
computed的特点
1、computed是基于 Object.defineProperty();
2、computed会产生缓存,如果依赖的数据不发生变化,不会重新计算;
computed和methods的区别
网友评论