本文介绍了观察计算属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下哈希的组件

I have a component with the following hash

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

我应该关注 isUserIDuserId 的变化吗?你能看计算属性吗?

Should I be watching isUserID or userId for changes? Can you watch computed properties?

推荐答案

是的,你可以设置 watcher 关于 computed 属性,请参阅 小提琴.

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}`);
        }
    }
});

这篇关于观察计算属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:26