首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有vuex模块装饰器的nuxtServerInit

带有vuex模块装饰器的nuxtServerInit
EN

Stack Overflow用户
提问于 2019-11-12 16:25:19
回答 2查看 2.1K关注 0票数 2

在Nuxt中使用这种方法:https://github.com/championswimmer/vuex-module-decorators#accessing-modules-with-nuxtjs

store/index.ts:

代码语言:javascript
复制
import { Store } from 'vuex'
import { initialiseStores } from '~/utils/store-accessor'

const initializer = (store: Store<any>) => initialiseStores(store)

export const plugins = [initializer]
export * from '~/utils/store-accessor'

utils/store-accessor.ts:

代码语言:javascript
复制
import { Store } from 'vuex'
import { getModule } from 'vuex-module-decorators'
import settingsModule from '~/store/settingsModule'

// eslint-disable-next-line
let settingsStore: settingsModule

// noinspection JSUnusedGlobalSymbols
function initialiseStores(store: Store<any>): void {
  settingsStore = getModule(settingsModule, store)
}

export { initialiseStores, settingsStore }

store/settingsModule.ts:

代码语言:javascript
复制
import { Action, Module, Mutation, VuexModule } from 'vuex-module-decorators'

@Module({
  name: 'settingsModule',
  namespaced: true,
  stateFactory: true
})
export default class settingsModule extends VuexModule {
  public videos: any[] = []

  @Mutation
  public SAVE_VIDEOS(value: any) {
    this.videos = value
  }

  @Action({ commit: 'SAVE_VIDEOS' })
  public saveVideos(value: any) {
    return value
  }

  // noinspection JSUnusedGlobalSymbols
  get videosArray() {
    return this.videos
  }
}

我如何调用nuxtServerInit()呢?如果我在“store/index.ts”中尝试:

代码语言:javascript
复制
export const actions: ActionTree<any, any> = {
  async nuxtServerInit({ dispatch }) {
    await dispatch('settingsModule/saveVideos', [1, 2, 3, 4, 5], { root: true })
  }
}

当我加载我的web-app时,nuxt没有触发动作调度。

EN

回答 2

Stack Overflow用户

发布于 2021-08-11 18:59:57

store/index.ts

代码语言:javascript
复制
// https://github.com/championswimmer/vuex-module-decorators#accessing-modules-with-nuxtjs
import { Store } from "vuex";
import { Context } from "@nuxt/types";
import { initialiseStores } from "~/utils/store-accessor";
export * from "~/utils/store-accessor";

export const actions = {
    nuxtServerInit(store: Store<any>, context:Context) {
        console.log(store);
        console.log(context);
    }
};
const initializer = (store: Store<any>) => initialiseStores(store);
export const plugins = [initializer];

const声明的顺序会有所不同

utils/store-accessor.ts

代码语言:javascript
复制
/* eslint-disable import/no-mutable-exports */
// https://github.com/championswimmer/vuex-module-decorators#accessing-modules-with-nuxtjs
import { Store } from "vuex";
import { getModule } from "vuex-module-decorators";
import HarmonyStoreModule from "~/store/harmony";

let harmonyStore: HarmonyStoreModule;

function initialiseStores(store: Store<any>): void {
    harmonyStore = getModule(HarmonyStoreModule, store);
}

export { initialiseStores, harmonyStore };
票数 1
EN

Stack Overflow用户

发布于 2020-03-27 21:05:20

确保您正在初始化您的存储:

代码语言:javascript
复制
import { Store } from "vuex";
import {initialiseStores, settingsStore } from "~/utils/store-accessor";

const initializer = (store: Store<any>) => initialiseStores(store);
export const plugins = [initializer];
export * from "~/utils/store-accessor";

export const actions = {
  async nuxtServerInit({}, ctx) {

// do it once more like so.. 
    initialiseStores(ctx.store);

// now just import stores from utils..
    settingsStore.saveVideos([1, 2, 3, 4, 5])

// NOTE: 
now all server side functions (asyncData, fetch etc..) will work without the need 
for you to initialize stores only because nuxtServerInit is the very first function 
called once server side. You'll need to call initialiseStores(ctx.store) once more client
side for everything to work (client side) correctly. I suggest using the module
nuxtClientInit and do the same.

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

https://stackoverflow.com/questions/58814678

复制
相关文章

相似问题

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