The difference between vuex directly modifying state and using commit to submit mutation to modify state:

1. When using vuex to modify the state, there are two ways:
  1) You can directly use $store.state.variable = xxx;
  2) this.$store.dispatch(actionType, payload)
       or this.$store.commit(commitType, Payload)


  
  2. Similarities and differences
  1) Common point: It can modify the variables in the state, and it is responsive (can trigger view updates)


  2) Differences:
   1. If you pass strict: true when creating a store in vue, turn it on Strict mode, then any operation that modifies the state, as long as it does not go through the
      mutation function, vue will throw err :[vuex] Do not mutate vuex store state outside mutation handlers.
      (For the reason for the error, please click: https://blog.csdn.net/zhq2005095/article/details/78359883 );
  
  3. Advantages of using commit to submit to mutation to modify state:      vuex can record every state change record and save it State snapshots for things like time roaming/rollback.      (Actually I haven't used it, and I haven't encountered the need to use this feature for the time being)   Conclusion: The official requirement is to set strict mode, and commit to modify the state every time, instead of directly modifying the state to facilitate debugging, etc.



  
 

            Hahaha, I don't, I just modify the state, I don't turn on strict mode.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324694654&siteId=291194637