How to listen for 'props' changes

前端 未结 13 1496
无人及你
无人及你 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:31

    For me this is a polite solution to get one specific prop(s) changes and create logic with it

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

    export default {
    name: 'getObjectDetail',
    filters: {},
    components: {},
    props: {
      objectDetail: { // <--- we could access to this value with this.objectDetail
        type: Object,
        required: true
      }
    },
    computed: {
      _objectDetail: {
        let value = false
        // ...
        // if || do || while -- whatever logic
        // insert validation logic with this.objectDetail (prop value)
        value = true
        // ...
        return value 
      }
    }
    
    

    So, we could use _objectDetail on html render

    <span>
      {{ _objectDetail }}
    </span>
    

    or in some method:

    literallySomeMethod: function() {
       if (this._objectDetail) {
       ....
       }
    }
    
    0 讨论(0)
提交回复
热议问题