Nuxt 2.12.2:使用新的 fetch 方法填充 store [英] Nuxt 2.12.2: Filling the store with the new fetch method
问题描述
当前文档中并不清楚,因为 fetch
方法发生了重大变化.据我了解,在 doc 中它说:
It is not clear in the current documentation, since the big change on the fetch
method. As I understand that in the doc it says:
fetch(context) 已被弃用,您可以在页面中使用匿名中间件: middleware(context)
fetch(context) has been deprecated, instead you can use an anonymous middleware in your page: middleware(context)
所以 context
不再可用?那么什么被传递到 new fetch
方法中?
So context
is no longer available? What is passed into the new fetch
method then?
以及如何访问 context
中的 store
?比如在2.12.2之前,我们可以使用fetch
方法如下:
And how do you access the store
in the context
? For example, prior to 2.12.2, we can use the fetch
method as follows:
// pages/index.vue
async fetch ({ store }) {
await store.dispatch('....')
},
因此,我认为上面的代码将来不会在 Nuxt 3 中很快运行.那你在一个页面的时候怎么填充店铺数据呢?
So, I assume that the code above will not be working soon in Nuxt 3 in the future. Then how do you fill the store data when you are on a page?
目前,您似乎仍然可以访问 context
作为 new fetch
中的第一个参数方法.未来会怎样?
Currently, it seems that you still can access the context
as the first argument in the new fetch
method. What about in the future?
推荐答案
然后什么被传递到 new
fetch
方法中?
fetch
钩子不再有任何参数.
The fetch
hook no longer has any arguments.
如何访问 context
中的 store
?
要访问 fetch
钩子中的上下文,请使用 this.$nuxt.context
;你可以像这样访问store
:
To access the context within the fetch
hook, use this.$nuxt.context
; and you can access the store
like this:
const { store } = this.$nuxt.context
store.dispatch(...)
// or
this.$nuxt.context.store.dispatch(...)
这篇关于Nuxt 2.12.2:使用新的 fetch 方法填充 store的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!