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 similar?
Update
As @wostex 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.
You can
watch
props to execute some code upon props changes: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:
and in html:
actually you have to be very careful when working on complex collections in such situations (passing down few times)
I work with a computed property like:
Resources is in this case a property:
Have you tried this ?
https://vuejs.org/v2/api/#watch
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.
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
Child.vue
Grandchild.vue
But after this you wont help notice the screaming warnings of vue saying
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.
if myProp is an object, it may not be changed in usual. so, watch will never be triggered. the reason of why myProp not be changed is that you just set some keys of myProp in most cases. the myProp itself is still the one. try to watch props of myProp, like "myProp.a",it should work.