我最近一直在努力在Vuex中实施模块。我无法找到有关我收到的控制台错误消息(rawModule is undefined
)的详细信息,所以我想我会分享我遇到的问题和解决方案。我正在做一个快速,简单的模块实现版本,因为我正在研究一些例子:
export const store = new Vuex.Store({
state: {
loggedIn: false,
user: {},
destination: ''
},
mutations: {
login: state => state.loggedIn = true,
logout: state => state.loggedIn = false,
updateUser: ( state, user ) => { state.user = user },
updateDestination: ( state, newPath ) => { state.destination = newPath }
},
modules: {
project
},
});
const project = {
state: {}
}
答案 0 :(得分:0)
问题最终是我在之后宣布我的模块我试图将它添加到Vuex商店。我原本以为可以通过变量吊装来宣布模块是可以的,但事实并非如此。以下是可行的代码:
const project = {
state: {}
}
export const store = new Vuex.Store({
state: {
loggedIn: false,
user: {},
destination: ''
},
mutations: {
login: state => state.loggedIn = true,
logout: state => state.loggedIn = false,
updateUser: ( state, user ) => { state.user = user },
updateDestination: ( state, newPath ) => { state.destination = newPath }
},
modules: {
project
},
});
希望这能节省一些人一些时间。我没有在文档中看到任何需要特定订购的内容,所以我很惊讶它很重要。如果有人对这种方式的原因有所了解,我真的很有兴趣听到它!也许是因为Vuex.Store()
函数在设置project
值之前被调用,所以项目模块的值被封装为undefined
,这会导致错误?