我正在开发一个 nuxt 应用程序,需要从 api 获取数据。数据只需要提供给登录用户。在我的 vuex 商店 index.js 中,我编写了以下代码:
async nuxtServerInit({ commit }) {
if (this.$auth.loggedIn) {
await Promise.all([
(async () => {
const response = await this.$axios.get('api/all/users')
commit('setUsers', response.data)
})(),
(async () => {
const response = await this.$axios.get('api/all/teams')
commit('setTeams', response.data)
})(),
(async () => {
const response = await this.$axios.get('api/all/clubs')
commit('setClubs', response.data)
})(),
])
}
},
以下工作,并在刷新时加载数据。但是 - 当用户登录时,nuxtserverinit 实际上不会触发,因为没有页面重新加载。处理它的正确方法是什么?