vuex的dispatch是異步執行的,所以如果有用到state的地方但是又沒有綁定組件的話就會導致渲染完成了但是數據沒有獲取到的情況
如何檢測state中的數據變化,下面舉個栗子
/*store.js*/
const state={
existSSID:{},
wifiList:[]
};
const getters={
getWiFiList: state => {
return state.wifiList;
}
};
export default new Vuex.Store({
state,
getters,
mutations,
actions
});
/在需要監聽的ssid.vue文件中/
computed:{
...mapGetters([
'getWiFiList'
])
},
watch: {
getWiFiList: function(li) { //li就是改變后的wifiList值
this.getAllId(li); //調用別的函數
console.log("改變",li);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35