Watching computed properties

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

I have a component with the following hash

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

2条回答
  •  -上瘾入骨i
    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}`);
            }
        }
    });
    

提交回复
热议问题