主要问题: 当页面刷新后会重新从浏览器缓存中读取恢复store,但是如下获取数据的方法有时会在store还没恢复前执行,导致从store中读取的数据为undefined
created() {
this.getOrders()
},
methods: {
async getOrders() {
let res = await this.$axios.get(`/getAllOrder/${this.touristId}/0`)
this.orderList = res.data.rows
}
},
记录我的一个稍微耗电性能的解决方法: 添加一个监听事件,当store恢复了computed获取的touristId也会同时更新,当数据不再是undefined时再重新请求一次
watch: {
touristId(newVal, oldVal) {
if (typeof newVal === "string") {
this.getOrders()
}
}
},
Q.E.D.