简体   繁体   中英

why should the state be changed only from mutation in vuex, vuejs app?

我想知道,如果我从其他位置直接更改状态,例如直接从组件或从getter函数更改状态,会发生什么?

Basically you could do that and probably all subscribers will be notified and nothing bad will happen, but the thing is that when you are using Vuex you agree to follow some design pattern proposed in there. Nothing would stop you from implementing your simple store (with only state and actions) that would share the state with vuejs and use it's reactivity. It would work- the thing is that Vuex is- as I said- a pattern that shows you the best way to track your changes and debug your app. In future your app's state may grow and I am assured that you would like to know what exactly is happening under the hood- thus you have mutations that have a simple task: update the state and help to track that exact change.

Using https://github.com/vuejs/vue-devtools you can easily see the transactions being done under the hood which would help others (or you in the future) what is going on with the state. Also, with mutations you are writing some kind of possibilities what in your state and how can be changed. Think of it as an API to access your data- no random or untrackable changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM