Sometimes, we want to create global data with Vue.js 2.
In this article, we’ll look at how to create global data with Vue.js 2.
How to create global data with Vue.js 2?
To create global data with Vue.js 2, we can create a Vuex store.
For instance, we write
const store = new Vuex.Store({
state: {
userData: [],
},
mutations: {
setUserData(state, data) {
state.userData = data;
},
},
});
to create a Vuex store with the Vuex.Store
constructor.
Then we can commit the setUserData
mutation with
store.commit('setUserData', userData)
And we can get the userData
state with
store.state.userData
Conclusion
To create global data with Vue.js 2, we can create a Vuex store.