主页

uniapp中的computed计算属性

冰与火之哥   IP属地: 青海
0 点赞 0阅读 605
用法一
<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;
    }
  }
}




© 著作权归作者所有,转载或内容合作请联系作者