小编nic*_*vic的帖子

如何在 nuxt 的 fetch 函数中调度存储操作?

我有一个使用 nuxt Universe SSR 的项目。我使用 fetch 方法获取布局上的数据,并触发存储操作调度。但这根本不起作用...

这是代码。这是默认布局文件:

import Navbar from '~/components/Navbar'

export default {
  components: {
    Navbar
  },
  async fetch ({ store }) {
    await store.dispatch('fetchItems')
  }
}
Run Code Online (Sandbox Code Playgroud)

这是 vuex 商店:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = () => new Vuex.Store({

  state: {
    allItems: [],
    currentCategories: []
  },
  mutations: {
    setItems (state, allItems) {
      state.allItems = allItems
    },
    setCurrentCategories (state, currentCategories) {
      state.currentCategories = currentCategories
    }
  },
  actions: {
    async fetchItems ({ commit, …
Run Code Online (Sandbox Code Playgroud)

fetch server-side-rendering vuex nuxt.js

6
推荐指数
2
解决办法
8029
查看次数

标签 统计

fetch ×1

nuxt.js ×1

server-side-rendering ×1

vuex ×1