Home  >  Q&A  >  body text

javascript - change the value of state in vue

I know that actions handle asynchronous processing. I don’t have asynchronous operations. What I mean is that after clicking a button, the value in the state is directly changed in the methods, without actions or mutations. Does this comply with the specifications of vue?

迷茫迷茫2662 days ago956

reply all(1)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-07-05 10:53:19

    About why you can’t directly call the mutation method or directly modify the state attribute, but you must submit the mutation through commit? Quote from official documentation:

    Once again, we submit mutations instead of directly changing store.state.count because we want to track state changes more clearly. This simple convention makes your intentions more obvious, so you can more easily interpret state changes within the application when reading the code. In addition, this also gives us the opportunity to implement some debugging tools that can record every state change and save state snapshots. With it, we can even achieve a debugging experience like time travel.
    Since the state in the store is responsive, calling the state in the store in the component is as simple as returning it in the calculated property. Triggering changes is just a matter of submitting mutations in the component's methods.

    It can be seen that when designing vuex, in order to track changes in state, it was agreed that commit must be used to submit changes in mutation-triggered data. You can try the official vue debugging plug-in, called Vue Devtools. You can directly see components, data, vuex, etc., and you can also track changes. If you try it, you will probably know why you do this.

    reply
    0
  • Cancelreply