<template>
<view>{{ computedProperty }}</view>
</template>
<script>
export default {
data() {
return {
// 依赖的数据
a: 1,
b: 2,
};
},
computed: {
computedProperty() {
// 根据依赖的数据计算出一个新的属性值
return this.a + this.b;
},
},
};
</script>
用法二
如果报“Computed property was assigned to but it has no setter“ 警告
computed: {
computedProperty: {
get() {
return this.a+this+b;
},
set(newValue) {
// 在这里处理值的变更
this.$emit('update:a', newValue); // 通知父组件
// 或者如果是本地状态:
// this.someLocalValue = newValue;
}
}
}