admin管理员组文章数量:1410705
What is the main difference between watch
and subscribe
and when I should use one over another? From Vuex official documentation, it seems both methods are doing the same thing and serving the same purpose, both methods have access to the state
object.
I am now having a state like below:
item: (state: any) => state.item
I also have an action to mit a mutation to change the value of item
, now I would like to know the new value of item
in some other ponent, which one I should use? watch
or subscribe
?
What is the main difference between watch
and subscribe
and when I should use one over another? From Vuex official documentation, it seems both methods are doing the same thing and serving the same purpose, both methods have access to the state
object.
I am now having a state like below:
item: (state: any) => state.item
I also have an action to mit a mutation to change the value of item
, now I would like to know the new value of item
in some other ponent, which one I should use? watch
or subscribe
?
1 Answer
Reset to default 9In the case that you want to do something when the value of item
changes, using watch
would be sufficient.
However, if you want to know how item
is changed specifically, you would want access to the mutation
object (which includes the type
of mutation, the payload
, and so on). In that case, you can use subscribe
.
One example can be that you mutate item
in 2 or more mutations and you only want to know the value of item
when mutation1
changed it.
Edit: I haven't tried this, but I think using subscribe
will call your function every time anything in the store is mutated.
本文标签: javascriptDifference between Vuex store WATCH and SUBSCRIBEStack Overflow
版权声明:本文标题:javascript - Difference between Vuex store WATCH and SUBSCRIBE - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745028655a2638359.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论