I have a plugin that adds some property to Vue instance.
Then I can access this property inside ponents using this.$plugin.prop
. How can I watch for its changes? I need to do something inside ponent based on this.$plugin.prop
value but neither watch
or this.$watch
worked for me. I assume its because watch
works in ponent context so I can't watch for variable outside ponent, for example
mounted() {
this.$watch('$plugin.prop', val => console.log(val));
}
doesn't work.
What is the right way to acplish this?
I have a plugin that adds some property to Vue instance.
Then I can access this property inside ponents using this.$plugin.prop
. How can I watch for its changes? I need to do something inside ponent based on this.$plugin.prop
value but neither watch
or this.$watch
worked for me. I assume its because watch
works in ponent context so I can't watch for variable outside ponent, for example
mounted() {
this.$watch('$plugin.prop', val => console.log(val));
}
doesn't work.
What is the right way to acplish this?
- 3 You can try to store plugin prop reference in data model of ponent, and then watch that property from the ponent data model - jsbin./kopucayoro/edit?html,js,output – Belmin Bedak Commented Jul 27, 2017 at 11:43
2 Answers
Reset to default 4Instead of mounted()
try
watch: {
'$plugin.prop': function(value){
console.log(value);
}
}
The offical documentation on watchers in the Vue docs
You may use puted properties instead like so:
puted: {
pluginChanged() {
console.log(this.$plugin.prop.val);
return this.$plugin.prop.val;
}
Read more about puted properties here.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745442306a4627884.html
评论列表(0条)