首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在只使用动态模块的情况下,如何使Nuxt.js操作的"nuxtServerInit“操作可见?

在只使用动态模块的情况下,如何使Nuxt.js操作的"nuxtServerInit“操作可见?
EN

Stack Overflow用户
提问于 2021-09-30 08:27:54
回答 1查看 184关注 0票数 0

根据this answer,下面是来自vuex-module-decorators official documentation的清单

代码语言:javascript
复制
// @/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的情况下必须是:

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
    }
  }
});
EN

回答 1

Stack Overflow用户

发布于 2021-10-09 13:34:37

nuxtServerInit只能在store/index.js文件中使用,这是服务器端操作,不能从客户端使用。

只有主模块(在store/index.js中)才会收到此操作。你需要从那里链接你的模块动作。nuxtServerInit操作如果在存储中定义了操作nuxtServerInit,并且模式是通用的,则Nuxt将通过上下文调用它(仅从服务器端调用)。当我们在服务器上有一些我们想要直接提供给客户端的数据时,它很有用。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69388835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档