我在 vuex 中有一个操作,我想在 vuex 本身而不是组件中自动调度。
我创建了一个通知栏,它通过多个页面上的不同通知进行更改。当我切换页面时,通知不是从头开始,而是创建了一个商店来设置要显示的通知。
我想从 vuex 内部而不是从组件内部调度 vuex 存储中的旋转函数
请注意:我正在使用 Nuxt
VUEX状态:store/notifications.js
export const state = () => ({
section: 0,
notifications: [
'notification 1',
'notification 2',
'notification 3'
]
})
export const mutations = {
INC_SECTION(state) {
state.section ++
},
RESET_SECTION(state) {
state.section = 0
}
}
export const actions = {
rotate({commit, dispatch, state}) {
setTimeout(() => {
let total = state.notifications.length -1
if (state.section === total) {
commit('RESET_SECTION')
}
else {
commit('INC_SECTION')
}
dispatch('rotate')
}, …Run Code Online (Sandbox Code Playgroud)