vue中如何computed对象属性值呢?

data(){
    return {
        a : {
            b: "测试",
            c: 10,
            d: 20,
            e: 20,
            f: 40
        }
    }
},
computed:{
    "a.b": function(){
        return this.a.c + this.a.d + this.a.e + this.a.f;
    }
}

请问下,如果对一个对象的属性进行监控呢?如上写法,当对象属性c、d、e、f变化时无法触发计算,要怎么写才对呢?

阅读 20.7k
3 个回答
computed:{
    computedA: function(){
        return {...this.a,b:this.a.c + this.a.d + this.a.e + this.a.f};
    }
}

计算属性不需要再data里声明,用的时候直接 this.b 就可以了

computed:{
    b: function(){
        return this.a.c + this.a.d + this.a.e + this.a.f;
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题