Javascript 为什么承诺都没有';我不能在vuex商店工作
我现在面临一个奇怪的问题。我的vue项目中有一个vuex存储区,它分为不同的模块。我想使用Javascript 为什么承诺都没有';我不能在vuex商店工作,javascript,vue.js,vuex,es6-promise,vuex-modules,Javascript,Vue.js,Vuex,Es6 Promise,Vuex Modules,我现在面临一个奇怪的问题。我的vue项目中有一个vuex存储区,它分为不同的模块。我想使用Promise.all()一次执行两个独立的异步vuex操作,以享受 商店/模块/类别: async CATEGORIES({ rootState }) { const response = await axios.post('link_to_api', { // some arguments for the api arg: root
Promise.all()
一次执行两个独立的异步vuex操作,以享受
商店/模块/类别:
async CATEGORIES({ rootState }) {
const response = await axios.post('link_to_api', {
// some arguments for the api
arg: rootState.args
})
return response
}
async TRANSPORTATION({ rootState }) {
const response = await axios.post('link_to_api', {
// some arguments for the api
arg: rootState.args
})
return response
}
async PUT_CATEGORIES({ commit, dispatch, rootState }) {
try {
const [resCategories, resTransportation] = await Promise.all([
dispatch('CATEGORIES').catch(err => { console.log('Fehler bei Kabinenabfrage!'); throw {error: err, origin: 'kabine'}; }),
dispatch('transportation/TRANSPORTATION', {root:true}).catch(err => { console.log('Fehler bei Flugabfrage!'); throw {error: err, origin: 'flug'}; })
])
//do something after both promises resolved
} catch(error) {
// do something if one promise rejected
commit('errorlog/ERROR', 4, {root:true})
dispatch("errorlog/LOG_ERROR", {'origin': '2', 'error_code': '113', 'message': error.toString()}, {root:true})
router.push({path: '/Error'})
}
商店/模块/运输:
async CATEGORIES({ rootState }) {
const response = await axios.post('link_to_api', {
// some arguments for the api
arg: rootState.args
})
return response
}
async TRANSPORTATION({ rootState }) {
const response = await axios.post('link_to_api', {
// some arguments for the api
arg: rootState.args
})
return response
}
async PUT_CATEGORIES({ commit, dispatch, rootState }) {
try {
const [resCategories, resTransportation] = await Promise.all([
dispatch('CATEGORIES').catch(err => { console.log('Fehler bei Kabinenabfrage!'); throw {error: err, origin: 'kabine'}; }),
dispatch('transportation/TRANSPORTATION', {root:true}).catch(err => { console.log('Fehler bei Flugabfrage!'); throw {error: err, origin: 'flug'}; })
])
//do something after both promises resolved
} catch(error) {
// do something if one promise rejected
commit('errorlog/ERROR', 4, {root:true})
dispatch("errorlog/LOG_ERROR", {'origin': '2', 'error_code': '113', 'message': error.toString()}, {root:true})
router.push({path: '/Error'})
}
我知道我想调用Promise中的异步函数。所有:
商店/模块/类别:
async CATEGORIES({ rootState }) {
const response = await axios.post('link_to_api', {
// some arguments for the api
arg: rootState.args
})
return response
}
async TRANSPORTATION({ rootState }) {
const response = await axios.post('link_to_api', {
// some arguments for the api
arg: rootState.args
})
return response
}
async PUT_CATEGORIES({ commit, dispatch, rootState }) {
try {
const [resCategories, resTransportation] = await Promise.all([
dispatch('CATEGORIES').catch(err => { console.log('Fehler bei Kabinenabfrage!'); throw {error: err, origin: 'kabine'}; }),
dispatch('transportation/TRANSPORTATION', {root:true}).catch(err => { console.log('Fehler bei Flugabfrage!'); throw {error: err, origin: 'flug'}; })
])
//do something after both promises resolved
} catch(error) {
// do something if one promise rejected
commit('errorlog/ERROR', 4, {root:true})
dispatch("errorlog/LOG_ERROR", {'origin': '2', 'error_code': '113', 'message': error.toString()}, {root:true})
router.push({path: '/Error'})
}
我得到以下错误:
这很奇怪,因为我使用了{root:true}
和dispatch中的前缀transport来访问存储中传输模块的操作。这对于我在catch块中使用的errorlog模块中的LOG_错误操作非常有用
如果我复制类别模块中的运输操作,效果会很好
以前有没有人遇到过这个问题并提出过建议
提前谢谢 在您的例子中,{root:true}
作为第二个参数传递,尽管它应该作为第三个参数传递
- dispatch('transportation/TRANSPORTATION', {root:true})
+ dispatch('transportation/TRANSPORTATION', null, {root:true})
据
要在全局命名空间中分派操作或提交变体,请将{root:true}作为第三个参数传递给dispatch and commit
它们还提供了一个示例代码(此处进一步简化)
把等待
和捕获
混在一起是很奇怪的,但是尝试/捕获
发送
是否返回承诺?或者它是否返回错误提示的未定义的
无法读取未定义的属性“catch”
错误与承诺无关。所有的都应该返回一个承诺,但它似乎没有连接到其他模块中的传输操作,因此未定义的@Jaromanda X0在某些条件和条件下返回承诺是非常奇怪的不是别人