How to listen for 'props' changes

前端 未结 13 1503
无人及你
无人及你 2020-11-30 17:27

In the VueJs 2.0 docs I can\'t find any hooks that would listen on props changes.

Does VueJs have such hooks like onPropsUpdated() or simi

相关标签:
13条回答
  • 2020-11-30 18:09

    Have you tried this ?

    watch: {
      myProp: {
        // the callback will be called immediately after the start of the observation
        immediate: true, 
        handler (val, oldVal) {
          // do your stuff
        }
      }
    }
    

    https://vuejs.org/v2/api/#watch

    0 讨论(0)
  • 2020-11-30 18:10

    You can use the watch mode to detect changes:

    Do everything at atomic level. So first check if watch method itself is getting called or not by consoling something inside. Once it has been established that watch is getting called, smash it out with your business logic.

    watch: { 
      myProp: function() {
       console.log('Prop changed')
      }
    }
    
    0 讨论(0)
  • 2020-11-30 18:10

    I use props and variables computed properties if I need create logic after to receive the changes

    export default {
    name: 'getObjectDetail',
    filters: {},
    components: {},
    props: {
        objectDetail: {
          type: Object,
          required: true
        }
    },
    computed: {
        _objectDetail: {
            let value = false
            ...
    
            if (someValidation)
            ...
        }
    }
    
    
    0 讨论(0)
  • 2020-11-30 18:14

    Not sure if you have resolved it (and if I understand correctly), but here's my idea:

    If parent receives myProp, and you want it to pass to child and watch it in child, then parent has to have copy of myProp (not reference).

    Try this:

    new Vue({
      el: '#app',
      data: {
        text: 'Hello'
      },
      components: {
        'parent': {
          props: ['myProp'],
          computed: {
            myInnerProp() { return myProp.clone(); } //eg. myProp.slice() for array
          }
        },
        'child': {
          props: ['myProp'],
          watch: {
            myProp(val, oldval) { now val will differ from oldval }
          }
        }
      }
    }
    

    and in html:

    <child :my-prop="myInnerProp"></child>
    

    actually you have to be very careful when working on complex collections in such situations (passing down few times)

    0 讨论(0)
  • 2020-11-30 18:15

    You need to understand, the component hierarchy you are having and how you are passing props, definitely your case is special and not usually encountered by the devs.

    Parent Component -myProp-> Child Component -myProp-> Grandchild Component

    If myProp is changed in parent component it will be reflected in the child component too.

    And if myProp is changed in child component it will be reflected in grandchild component too.

    So if myProp is changed in parent component then it will be reflected in grandchild component. (so far so good).

    Therefore down the hierarchy you don't have to do anything props will be inherently reactive.

    Now talking about going up in hierarchy

    If myProp is changed in grandChild component it won't be reflected in the child component. You have to use .sync modifier in child and emit event from the grandChild component.

    If myProp is changed in child component it won't be reflected in the parent component. You have to use .sync modifier in parent and emit event from the child component.

    If myProp is changed in grandChild component it won't be reflected in the parent component (obviously). You have to use .sync modifier child and emit event from the grandchild component, then watch the prop in child component and emit an event on change which is being listened by parent component using .sync modifier.

    Let's see some code to avoid confusion

    Parent.vue

    <template>
        <div>
        <child :myProp.sync="myProp"></child>
        <input v-model="myProp"/>
        <p>{{myProp}}</p>
    </div>
    </template>
    
    <script>
    
        import child from './Child.vue'
    
        export default{
            data(){
                return{
                    myProp:"hello"
                }
            },
            components:{
                child
            }
        }
    </script>
    
    <style scoped>
    </style>
    

    Child.vue

    <template>
    <div>   <grand-child :myProp.sync="myProp"></grand-child>
        <p>{{myProp}}</p>
    </div>
    
    </template>
    
    <script>
        import grandChild from './Grandchild.vue'
    
        export default{
            components:{
                grandChild
            },
            props:['myProp'],
            watch:{
                'myProp'(){
                    this.$emit('update:myProp',this.myProp)
    
                }
            }
        }
    </script>
    
    <style>
    
    </style>
    

    Grandchild.vue

    <template>
        <div><p>{{myProp}}</p>
        <input v-model="myProp" @input="changed"/>
        </div>
    </template>
    
    <script>
        export default{
            props:['myProp'],
            methods:{
                changed(event){
                    this.$emit('update:myProp',this.myProp)
                }
            }
        }
    </script>
    
    <style>
    
    </style>
    

    But after this you wont help notice the screaming warnings of vue saying

    'Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders.'

    Again as I mentioned earlier most of the devs don't encounter this issue, because it's an anti pattern. That's why you get this warning.

    But in order to solve your issue (according to your design). I believe you have to do the above work around(hack to be honest). I still recommend you should rethink your design and make is less prone to bugs.

    I hope it helps.

    0 讨论(0)
  • 2020-11-30 18:16

    He,

    in my case I needed a solution where anytime any props would change, I needed to parse my data again. I was tired of making seperated watcher for all my props, so I used this:

      watch: {
        $props: {
          handler() {
            this.parseData();
          },
          deep: true,
          immediate: true,
        },
    

    Key point to take away from this example is to use deep: true so it not only watches $props but also it's nested values like e.g. props.myProp

    You can learn more about this extended watch options here: https://vuejs.org/v2/api/#vm-watch

    0 讨论(0)
提交回复
热议问题