阅读设置
界面、侧栏和正文分别调整,自动保存。
主题切换
界面、侧栏和正文分别调整,自动保存。
npm install vuex@next --saveimport Vuex from 'vuex'
export default Vuex.createStore({
// 1. 共享数据
state: {
count: 0,
username: ""
},
// 2. 同步修改 state
mutations: {
SET_COUNT(state, num) {
state.count = num
},
SET_USER(state, name) {
state.username = name
}
},
// 3. 异步操作
actions: {
asyncSetCount({ commit }, num) {
setTimeout(() => {
commit('SET_COUNT', num)
}, 1000)
}
},
// 4. 加工数据
getters: {
doubleCount(state) {
return state.count * 2
}
}
})this.$store.state.count
this.$store.state.usernamethis.$store.commit('SET_COUNT', 100)this.$store.dispatch('asyncSetCount', 200)this.$store.getters.doubleCount