在VueJs 2.0文档中,我找不到任何可以监听道具变化的钩子。

VueJs有这样的钩子像onpropsupdate()或类似的吗?

更新

正如@wostex建议的那样,我试着观察我的财产,但没有任何变化。然后我意识到我遇到了一个特殊的情况:

<template>
    <child :my-prop="myProp"></child>
</template>

<script>
   export default {
      props: ['myProp']
   }
</script>

我将父组件接收到的myProp传递给子组件。然后手表:{myProp:…不起作用。


当前回答

如果你的道具myProp有嵌套项,这些嵌套项不会是反应性的,所以你需要使用像lodash deepClone这样的东西:

<child :myProp.sync="_.deepClone(myProp)"></child>

就这样,不需要监视或其他任何东西。

其他回答

如果myProp是一个对象,通常不可以更改。所以,手表永远不会被触发。为什么myProp不被改变的原因是你在大多数情况下只设置myProp的一些键。myProp本身仍然是一个。 试着看myProp的道具,比如“myProp”。A "应该有用。

道具和v型模型处理。如何将值从父对象传递给子对象,再从子对象传递给父对象。

不需要手表!另外,在Vue中改变道具也是一种反模式,所以永远不要改变子组件或组件中的道具值。使用$emit更改值,Vue将始终按预期工作。

/* COMPONENT - CHILD */ Vue.component('props-change-component', { props: ['value', 'atext', 'anumber'], mounted() { var _this = this this.$emit("update:anumber", 6) setTimeout(function () { // Update the parent binded variable to 'atext' _this.$emit("update:atext", "4s delay update from child!!") }, 4000) setTimeout(function () { // Update the parent binded v-model value _this.$emit("input", "6s delay update v-model value from child!!") }, 6000) }, template: '<div> \ v-model value: {{ value }} <br> \ atext: {{ atext }} <br> \ anumber: {{ anumber }} <br> \ </div>' }) /* MAIN - PARENT */ const app = new Vue({ el: '#app', data() { return { myvalue: 7, mynumber: 99, mytext: "My own text", } }, mounted() { var _this = this // Update our variable directly setTimeout(function () { _this.mytext = "2s delay update mytext from parent!!" }, 2000) }, }) <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <props-change-component v-model='myvalue' :atext.sync='mytext' :anumber.sync='mynumber'> </props-change-component> </div>

我认为在大多数情况下,Vue会在道具变化时更新组件的DOM。

如果这是你的情况,那么你可以使用beforeUpdate()或updated()钩子(docs)来观察道具。

如果你只对新val感兴趣而不需要旧val,你可以这样做

new Vue({ el: '#app', data: { text: '' }, components: { 'child': { template: `<p>{{ myprop }}</p>`, props: ['myprop'], beforeUpdate() { console.log(this.myprop) }, updated() { console.log(this.myprop) } } } }); <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <child :myprop="text"></child> <input v-model="text" placeholder="Type here to view prop changes" style="width:20em"> </div>

你可以观察道具,在道具改变时执行一些代码:

new Vue({ el: '#app', data: { text: 'Hello' }, components: { 'child' : { template: `<p>{{ myprop }}</p>`, props: ['myprop'], watch: { myprop: function(newVal, oldVal) { // watch it console.log('Prop changed: ', newVal, ' | was: ', oldVal) } } } } }); <script src="https://unpkg.com/vue/dist/vue.js"></script> <div id="app"> <child :myprop="text"></child> <button @click="text = 'Another text'">Change text</button> </div>

对一些用例的有趣观察。

如果您通过道具从存储中监视数据项,并且在同一存储中多次更改数据项,则不会监视该数据项。

但是,如果您将数据项更改分离为相同突变的多个调用,它将被监视。

This code will NOT trigger the watcher: // Somewhere in the code: this.$store.commit('changeWatchedDataItem'); // In the 'changeWatchedDataItem' mutation: state.dataItem = false; state.dataItem = true; This code WILL trigger the watcher at each mutation: // Somewhere in the code: this.$store.commit('changeWatchedDataItem', true); this.$store.commit('changeWatchedDataItem', false); // In the 'changeWatchedDataItem' mutation: changeWatchedDataItem(state, newValue) { state.dataItem = newValue; }