Watching computed properties

后端 未结 2 1822
Happy的楠姐
Happy的楠姐 2020-12-09 01:40

I have a component with the following hash

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

相关标签:
2条回答
  • 2020-12-09 01:43
    computed: {
      name: {
        get: function(){
          return this.name;
        }
      }
    },
    watch: {
      name: function(){
        console.log('changed');
      }
    }
    

    This way we can watch over the computed property if it is changed we get notified on the console.

    0 讨论(0)
  • 2020-12-09 02:07

    Yes, you can setup watcher on computed property, see the fiddle.

    Following is the code to set watch on computed property:

    const demo = new Vue({
        el: '#demo',
    
        data() {
            return {
                age: ''
            };
        },
    
        computed: {
            doubleAge() {
                return 2 * this.age;
            }
        },
    
        watch: {
            doubleAge(newValue) {
                alert(`yes, computed property changed: ${newValue}`);
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题