javascript - How to watch for Vue instance property inside component? - Stack Overflow

I have a plugin that adds some property to Vue instance.Then I can access this property inside ponents

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?

Share Improve this question asked Jul 27, 2017 at 11:01 user0103user0103 1,2723 gold badges18 silver badges36 bronze badges 1
  • 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
Add a ment  | 

2 Answers 2

Reset to default 4

Instead 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信