使用 vue 状态重新加载页面后,获取的数据消失



正在从 API 获取数据 这是商店中的操作

fetchVisits({commit}) {
axios.get('/api/visits')
.then(res => {
commit('FETCH_VISITS', res.data)
}).catch(err => {
console.log(err)
})
},
}

突变

FETCH_VISITS(state, visits) {
return state.visits = visits
},

state: {
visits:[]
},

吸气剂

getters: {
visits: state => {
return state.visits
}
},

调度操作

created() {
this.$store.dispatch('fetchVisits')
},

但是当我刷新页面时,要显示的数据消失了

操作由 store.dispatch 方法触发。现在,在您的情况下,您可能错过了调度 fetchVisit 操作,或者也可能,您在不是您的主要组件的其他组件中调度了该操作。这就是为什么当您点击重新加载时,调度方法没有被调用。为了解决这个问题.

您必须在主组件中调度 fetchVisit 操作,以便在每次重新加载页面时调用它。或者,您再次将该操作调度到您所说的状态为空的组件本身。 这就是您的调度方式

this.store.dispatch('fetchVisits'(

最新更新