简体   繁体   English

一起使用 vuex-module-decorators 和 @nuxtjs/auth 时出错

[英]Error when using vuex-module-decorators and @nuxtjs/auth together

I am using nuxtjs with typescript and use vuex-module-decorators.我正在使用带有 typescript 的 nuxtjs 并使用 vuex-module-decorators。 but I get error when add @nuxtjs/auth to my project.但是将@nuxtjs/auth添加到我的项目时出现错误。

Uncaught (in promise) Error: ERR_STORE_NOT_PROVIDED: To use getModule(), either the module should be decorated with store in decorator, ie @Module({store: store}) or store should be passed when calling getModule(), ie getModule(MyModule, this.$store) Uncaught (in promise) 错误:ERR_STORE_NOT_PROVIDED: 要使用 getModule(),模块应该在装饰器中使用 store 进行装饰,即 @Module({store: store}) 或者调用 getModule() 时应该传递 store,即 getModule( MyModule, this.$store)

This error happen when call Action.调用 Action 时会发生此错误。

When @nuxtjs/auth from modules it's ok.当模块中的@nuxtjs/auth问题。

store/index.ts存储/index.ts

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

utils/store-accessor实用程序/商店访问器

/* eslint-disable import/no-mutable-exports */
import { Store } from "vuex";
import { getModule } from "vuex-module-decorators";
import { NuxtAxiosInstance } from "@nuxtjs/axios";
import Login from "~/store/Login";
import App from "~/store/App";

let $axios: NuxtAxiosInstance;

function initializeAxios(axiosInstance: NuxtAxiosInstance) {
    $axios = axiosInstance;
}

let loginStore: Login, appStore: App;

function initializeStores(store: Store<any>): void {
    loginStore = getModule(Login, store);
    appStore = getModule(App, store);
}

export { initializeStores, initializeAxios, loginStore, appStore, $axios };

I add the plugin/auth.js like this:我像这样添加插件/auth.js:

import { initialiseStores } from "~/utils/store-accessor";

export default function ({ $auth, store }) {
  initialiseStores(store); 

  $auth.onError((error, name, endpoint) => {
    console.log(name, error, endpoint);
  });

  $auth.onRedirect((to, from) => {
    // you can optionally change `to` by returning a new value
  });
}

Then, you can execute actions in the component, this way:然后,您可以通过以下方式在组件中执行操作:

import moduleName from "@/store"

...

methods: {
  m() {
    moduleName.someAction()
  }
}

I add vuex: false to auth in nuxt.config.js add error gone, but now I do not access to to $auth.user in components.我将vuex: false添加到nuxt.config.js中的auth添加错误消失了,但现在我无法访问组件中的$auth.user So I do it by my self and create user store in vuex and manually get user info.所以我自己做,在 vuex 中创建用户存储并手动获取用户信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM