简体   繁体   English

为什么只应从vuex,vuejs应用程序中的突变更改状态?

[英]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. 基本上,您可以这样做,并且可能会通知所有订阅者,并且不会发生任何不好的情况,但是事实是,当您使用Vuex时,您同意遵循那里提出的某些设计模式。 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. 没有什么能阻止您实现将简单的存储(仅包含状态和操作)与vuejs共享状态并使用其反应性的功能。 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. 正如我所说,Vuex是可行的-一种模式,向您展示跟踪更改和调试应用程序的最佳方法。 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. 使用https://github.com/vuejs/vue-devtools,您可以轻松地看到幕后进行的事务,这将帮助其他人(或您将来的事务)状态的发展。 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. 可以将其视为访问数据的API-无需进行随机或无法跟踪的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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