Nuxt 2.12.2:使用新的 fetch 方法填充 store

lau*_*kok 1 vue.js nuxt.js nuxtjs

当前文档中尚不清楚,因为fetch方法发生了重大变化。据我了解,在文档中它说:

fetch(context) 已被弃用,您可以在页面中使用匿名中间件: middleware(context)

所以context已经不能用了?那么什么被传递到 fetch方法中呢?

你如何进入storecontext?比如在2.12.2之前,我们可以使用fetch如下方法:

// pages/index.vue
async fetch ({ store }) {
  await store.dispatch('....')
},
Run Code Online (Sandbox Code Playgroud)

因此,我认为上面的代码将来不会在 Nuxt 3 中很快运行。那你在一个页面的时候怎么填充店铺数据呢?

目前,似乎你仍然可以访问context第一个参数 fetch方法。将来怎么办?

ton*_*y19 5

那么什么是传递给 fetch方法的呢?

fetch钩不再有任何参数。

你如何访问store中的context

要访问fetch钩子内的上下文,请使用this.$nuxt.context; 你可以store像这样访问:

const { store } = this.$nuxt.context
store.dispatch(...)

// or
this.$nuxt.context.store.dispatch(...)
Run Code Online (Sandbox Code Playgroud)