【发布时间】:2019-06-16 11:26:07
【问题描述】:
我在 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')
}, 3500)
}
}
export default {
state,
mutations,
actions
}
VUE JS 组件:
<template>
<section class="notifications">
<template v-for="(notification, i) in notifications" >
<p v-if="$store.state.notifications.section === i" :key="i">{{notification}}</p>
</template>
</section>
</template>
<script>
export default {
data() {
return { notifications: [] }
},
computed: {
setData() {
this.notifications = this.$store.state.notifications.notifications
}
},
created() {
this.setData
}
}
</script>
【问题讨论】:
标签: javascript vue.js vuex nuxt.js