gpt4 book ai didi

vue.js - 有没有办法在两个命名空间的 vuex 模块之间分派(dispatch)操作?

转载 作者:搜寻专家 更新时间:2023-10-30 22:07:15 25 4
gpt4 key购买 nike

是否可以在命名空间模块之间分派(dispatch)操作?

例如我有 Vuex 模块“游戏板”和“通知”。每个都有命名空间。我想从通知模块中的游戏板模块发送一个 Action 。

我想我可以像这样在调度操作名称中使用模块名称:

// store/modules/gameboard.js
const actions = {
myaction ({dispatch}) {
...
dispatch('notification/triggerSelfDismissingNotifcation', {...})
}
}
// store/modules/notification.js
const actions = {
triggerSelfDismissingNotification (context, payload) {
...
}
}

但是当我尝试这样做时,我得到了错误,让我认为 Vuex 正在尝试在我的游戏板模块中调度一个 Action :

[vuex] unknown local action type: notification/triggerSelfDismissingNotification, global type: gameboard/notification/triggerSelfDismissingNotification

有没有一种方法可以将给定 Vuex 模块的 Action 分派(dispatch)到另一个 Vuex 模块,或者我是否需要在根 Vuex 实例中创建某种桥接器?

最佳答案

你只需要指定你从根上下文中调度:

// from the gameboard.js vuex module
dispatch('notification/triggerSelfDismissingNotifcation', {...}, {root:true})

现在,当分派(dispatch)到达根时,它将具有通向通知模块的正确命名空间路径(相对于根实例)。

这是假设您在 Vuex 存储模块上设置 namespaced: true

关于vue.js - 有没有办法在两个命名空间的 vuex 模块之间分派(dispatch)操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42984132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com