代码之家  ›  专栏  ›  技术社区  ›  vimuth

停止Vue JS监视属性监视子对象

  •  0
  • vimuth  · 技术社区  · 7 年前

    嗨这是我的密码,

            var vm = new Vue({
                    el: '#el',
                    data: {
                        input: {
                            sorting: "",
                            brand: null,
                            model: null,
                            country: "all",
                            cap: "",
                            radius: ""         
                        }
                    },
                    watch: {
                        input: {
                            handler(newInput) {
    
                            },
                            deep: true
                        }  
                    }
                });
    

    这里我在观察天气输入对象的变化。但我只需要看一些项目只输入对象。例如 如果input.country发生变化,我需要做些什么,但input.brand发生变化时,我不需要做。 不幸的是,我的代码很复杂,不能从输入对象中取出项目。

    2 回复  |  直到 7 年前
        1
  •  0
  •   Jeff    7 年前

    注意你需要什么:

                watch: {
                    'input.country': {
                        handler(newCountry) {
    
                        }
                    }  
                }
    
        2
  •  0
  •   Thomas Brd    7 年前

    声明一 computed 重视谁是目标 this.item.country :

    computed: {
        itemCountry() {
            return this.item.country;
        }
    }
    

    看看这个新的计算值:

    watch: {
        itemCountry: {
            immediate: true,
            handler(newInput) {
                // do your stuff
            }
        }
    }
    
    推荐文章