Vuex: Understanding the usage of Store and vuexstore
1. What is Store?
As mentioned in the previous article,Vuex
Is to provide a warehouse,Store
Many objects are stored in the repository. The state is the data source storage location, correspondingVue
Objectdata
(As mentioned lateractions
Andmutations
Correspondsmethods
).
In useVuex
UsuallyStore
Instancenew Vuex.store({state,getters,mutations,actions})
Many sub-modules are also used.modules
.
Conclusion,Store
Class is the repository for data storage and management methods. The implementation method is to pass data and methods into its instance in the form of objects. Note that only one application or project can exist.Store
Instance !!
2. Store source code analysis
Class Store {constructor (options ={}) {// 1. part 2 'assert (Vue, 'must call Vue. use (Vuex) before creating a store instance. ') // make sure that the Vue exists assert (typeof Promise! = 'Undefined', 'vuex requires a Promise polyfill in this browser. ') // ensure that promise exists // 2. get the state, plugins, and strict const {state ={}, // rootState plugins = [], // plug-in strict = false // whether strict mode} = options // 3. store internal state to create the internal store attribute this. _ options = options // storage parameter this. _ committing = false // identify the submission status. Make sure that the state can be changed only in mutation and not outside. _ actions = Object. create (null) // store user-defined actions this. _ mutations = Object. create (null) // store user-defined mutations this. _ wrappedGetters = Object. create (null) // store user-defined getters this. _ runtimeModules = Object. create (null) // store the runtime modules this. _ subscribers = [] // stores all subscribers with mutation congestion changes this. _ watcherVM = new Vue () // use the Vue instance method and $ watch to observe changes. // 4. direct this of dispatch and commit to the current store instance const store = this const {dispatch, commit} = this. dispatch = function boundDispatch (type, payload) {return dispatch. call (store, type, payload)} this. commit = function boundCommit (type, payload, options) {return commit. call (store, type, payload, options )}}
Next, we will analyze each module step by step.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.