在仅使用动态模块的情况下,如何使Nuxt.js操作的"nuxtServerInit"操作可见? [英] How to make visible the "nuxtServerInit" action for Nuxt.js action in the case with dynamic modules only?

查看:23
本文介绍了在仅使用动态模块的情况下,如何使Nuxt.js操作的"nuxtServerInit"操作可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据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);
    }
  }
});

推荐答案

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

只有主模块(在store/index.js中)才会收到此操作。您需要从那里链接模块操作。

nuxtServerInit操作 如果在存储中定义了操作nuxtServerInit,并且模式是通用的,则Nuxt将使用上下文(仅从服务器端)调用它。当我们希望将服务器上的某些数据直接提供给客户端时,它非常有用。

这篇关于在仅使用动态模块的情况下,如何使Nuxt.js操作的&quot;nuxtServerInit&quot;操作可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆