根据this answer,下面是来自vuex-module-decorators official documentation的清单
// @/store/index.ts
import Vuex from 'vuex'
const store = new Vuex.Store({
/*
Ideally if all your modules are dynamic
then your store is registered initially
as a completely empty object
*/
})在Nuxt的情况下必须是:
import Vue from "vue";
import Vuex, { Store } from "vuex";
Vue.use(Vuex);
export const store: Store<unknown> = new Vuex.Store<unknown>({});但是,如何将nuxtServerInit操作集成到上述方法中呢?在下面的store/index.ts列表中,不会调用nuxtServerInit。
import Vue from "vue";
import Vuex, { Store } from "vuex";
Vue.use(Vuex);
export const store: Store<unknown> = new Vuex.Store<unknown>({
actions: {
nuxtServerInit(context: unknown): void {
console.log("Called !");
console.log(context);
}
}
});发布于 2021-10-09 13:34:37
nuxtServerInit只能在store/index.js文件中使用,这是服务器端操作,不能从客户端使用。
只有主模块(在store/index.js中)才会收到此操作。你需要从那里链接你的模块动作。nuxtServerInit操作如果在存储中定义了操作nuxtServerInit,并且模式是通用的,则Nuxt将通过上下文调用它(仅从服务器端调用)。当我们在服务器上有一些我们想要直接提供给客户端的数据时,它很有用。
https://stackoverflow.com/questions/69388835
复制相似问题