vuex状态更新无法被检测到

项目中遇到这样一种情况, AJAX获取到的数据作为缓存,同样的参数请求不再发生实质的AJAX请求
但页面上的表现形式仍然要和真的发生请求一样

vuex

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const REQUEST = 'GET USER DATA REQUEST';
const SUCCESS = 'GET USER DATA SUCCESS';

const state = {
onFetch: false,
data: {}
};
const actions = {
getData({ commit, state } , id) {
if(Object.keys(state.data).includes(id)) {
commit(REQUEST);
commit(SUCCESS);
}
}
};

const mutations = {
[REQUEST](state) {
state.onFetch = true;
},
[SUCCESS](state) {
state.onFetch = false;
}
};

view

1
2
3
4
5
export default {
watch: {
// ...
},
};

这时候发现页面无法监控到onFetch的变化,所以暂时只能用以下的方法解决

1
2
3
setTimeout(() => {
commit(SUCCESS)
})
#

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×