观察计算属性

时间:2016-12-09 19:14:49

标签: vue-component vuejs2 computed-properties

我有一个包含以下哈希的组件

{ 
  computed: { 
    isUserID: { 
      get: function(){
         return this.userId? 
      } 
  }
}

我应该关注isUserIDuserId进行更改吗?你能看一下计算机属性吗?

2 个答案:

答案 0 :(得分:60)

是的,您可以在watcher媒体资源上设置computed,请参阅fiddle

以下是在计算属性上设置监视的代码:

var demo = new Vue({
    el: '#demo',
    data: function(){
        return {
        age: ''
      };
    },
    computed: {
      doubleAge: function () {
            return 2*this.age
        }
    },
    watch: {
      doubleAge: function (val) {
         alert("yes, computed property changed")
       }
    }   
})

答案 1 :(得分:2)

computed: {
  name: {
    get: function(){
      return this.name;
    }
  }
},
watch: {
  name: function(){
    console.log('changed');
  }
}

这样我们可以监视计算属性,如果它被更改,我们会在控制台上得到通知。

相关问题