无法通过承诺在 Vuex 中使用 getter 获取存储状态
Cannot get store states with getters in Vuex with promise then
因为我想检查他们进入的频道路由是否需要PIN码,所以我使用router.beforeEach如下:
router.beforeEach((to, from, next) => {
if(to.path == '/') {
next();
}else {
store.dispatch('checkChannelhasPin', to.params.id).then(()=>{
console.log(store.getters);
console.log(store.getters.ispin);
setTimeout(() => {
console.log(store.getters.ispin);
}, 500);
})
}
}
作为console.log
,我有三个结果:
问题是,我在检查 'checkChannelhasPin' 后无法获得 stateispin。
操作:
import axios from 'axios';
import setChannelAuthToken from '../../utils/setChannelAuthToken';
import {
CHECK_CHANNEL_HASPIN
} from '../typeName';
const state = {
ispin: true,
}
const getters = {
ispin: (state) => state.ispin
};
const actions = {
async checkChannelhasPin({commit}, params) {
axios.post(
window.home+'/json/channel/checkAuth',
{
channel_id:params
}
).then((response) => {
let payload = response.data.ispin;
commit(CHECK_CHANNEL_HASPIN, payload);
}).catch( (error) => {
console.log(error);
});
}
}
const mutations = {
CHECK_CHANNEL_HASPIN: (state, payload) => (state.ispin = payload)
};
export default {
state,
getters,
actions,
mutations
};
你能推荐我吗?非常感谢。
您需要return 行动中的承诺:
checkChannelhasPin({commit}, params) {
return axios.post(
window.home+'/json/channel/checkAuth',
{
channel_id:params
}
).then((response) => {
let payload = response.data.ispin;
commit(CHECK_CHANNEL_HASPIN, payload);
}).catch( (error) => {
console.log(error);
});
}
或者,如果您使用 async/await
语法,您可以这样做:
async checkChannelhasPin({commit}, params) {
try {
const response = await axios.post(
window.home+'/json/channel/checkAuth',
{
channel_id:params
}
)
let payload = response.data.ispin;
commit(CHECK_CHANNEL_HASPIN, payload);
} catch(error) {
console.log(error);
}
}
当您使用 .then
时,您必须明确 return 承诺。当您使用 async/await
时,您不需要。您使用的是 async
关键字,但使用的是 .then
而不是 await
。
因为我想检查他们进入的频道路由是否需要PIN码,所以我使用router.beforeEach如下:
router.beforeEach((to, from, next) => {
if(to.path == '/') {
next();
}else {
store.dispatch('checkChannelhasPin', to.params.id).then(()=>{
console.log(store.getters);
console.log(store.getters.ispin);
setTimeout(() => {
console.log(store.getters.ispin);
}, 500);
})
}
}
作为console.log
,我有三个结果:
问题是,我在检查 'checkChannelhasPin' 后无法获得 stateispin。
操作:
import axios from 'axios';
import setChannelAuthToken from '../../utils/setChannelAuthToken';
import {
CHECK_CHANNEL_HASPIN
} from '../typeName';
const state = {
ispin: true,
}
const getters = {
ispin: (state) => state.ispin
};
const actions = {
async checkChannelhasPin({commit}, params) {
axios.post(
window.home+'/json/channel/checkAuth',
{
channel_id:params
}
).then((response) => {
let payload = response.data.ispin;
commit(CHECK_CHANNEL_HASPIN, payload);
}).catch( (error) => {
console.log(error);
});
}
}
const mutations = {
CHECK_CHANNEL_HASPIN: (state, payload) => (state.ispin = payload)
};
export default {
state,
getters,
actions,
mutations
};
你能推荐我吗?非常感谢。
您需要return 行动中的承诺:
checkChannelhasPin({commit}, params) {
return axios.post(
window.home+'/json/channel/checkAuth',
{
channel_id:params
}
).then((response) => {
let payload = response.data.ispin;
commit(CHECK_CHANNEL_HASPIN, payload);
}).catch( (error) => {
console.log(error);
});
}
或者,如果您使用 async/await
语法,您可以这样做:
async checkChannelhasPin({commit}, params) {
try {
const response = await axios.post(
window.home+'/json/channel/checkAuth',
{
channel_id:params
}
)
let payload = response.data.ispin;
commit(CHECK_CHANNEL_HASPIN, payload);
} catch(error) {
console.log(error);
}
}
当您使用 .then
时,您必须明确 return 承诺。当您使用 async/await
时,您不需要。您使用的是 async
关键字,但使用的是 .then
而不是 await
。