鍍金池/ 問答/HTML/ 父組件監(jiān)聽路由變化,將query通過props推送到子組件,子組件watch沒有

父組件監(jiān)聽路由變化,將query通過props推送到子組件,子組件watch沒有捕獲變化?

parent

<component v-if="DyComp" v-bind:is="DyComp.type" :dataIn="DyComp.data"></component>
    watch: {
        $route: function() {
            console.log("route change");
            var dataUri = this.$route.query.dataUri+'';
            this.DyComp.data.dataUri = dataUri;
            console.log(this.$route.query.dataUri)
        }
    },

child

    props: ["dataIn"],
        watch: {
        dataIn: function() {
            var dataUri = this.dataIn.dataUri;
            console.warn(dataUri, "reload data by dataUri");
            var vm = this;
            if (dataUri) {
                axios
                    .get("/static/baiyunwsz/" + dataUri + ".html")
                    .then(function(res) {
                        console.log(res);
                        vm.article = res.data;
                        // return false;
                    })
                    .catch(function(err) {
                        console.log(err);
                    });
            } else {
                console.log("no dataUri");
                this.fetchData();
            }
        }
    },

父組件watch在路由變化時輸出正確
this.DyComp也改變了
但是子組件的watch沒有跑起來,請問是什么原因?

回答
編輯回答
法克魷

因為你在父組件中只是更新了porp的一個屬性,所以需要再子組件監(jiān)聽中加上 deep: true, 這樣會監(jiān)聽各個attribute變化

2017年3月17日 00:01