VueJS 2.0 - Can't hook a component on props up

2019-09-16 05:41发布

I need to force re-render component on update to be able to call FB.XFBML.parse(). However, no matter what I do I am not able to invoke any lifecycle hook beforeUpdate/updated when a property updated. (According to docs it only listens for data updates which props obviously aren't).

<template>
  <div>
    {{myProp}}
  </div>
</template>

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

    watch: {
       myprop: function(newVal, oldVal) { // watch it
            console.log('Prop changed: ', newVal, ' | was: ', oldVal)
       }           
    },

    updated(){ // never invoked},

    beforeUpdate(){ // never invoked }

  }
</script>

I can see that the property channel changed when sending different values from a parent component.

Is there a hook to listen for props changes?

Update

As @chris suggested, I tried to watch my property but nothing changed. Then I realized that I've got a special case:

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

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

I am passing myProp that the parent component receives to the child component. Then the watch: {myProp: ...} is not working.

标签: vue.js vuejs2
1条回答
Summer. ? 凉城
2楼-- · 2019-09-16 06:20

What you're looking for is watch:

<script>
  export default {
    props: ['channel'],

    watch: {
      channel(newValue) {
        console.log(newValue)
      }
    }
  }
</script>
查看更多
登录 后发表回答