未注册动态和命名空间模块(vuex-module-decorators,vuex-class)

时间:2019-04-02 08:38:21

标签: vue.js vuex nuxt.js vuex-modules

该模块未注册:

$nuxt.$store._modulesNamespaceMap // No properties

此外,我收到此警告:

Classic mode for store/ is deprecated and will be removed in Nuxt 3.

我尝试过的事情:

  1. 手动加载模块以查看名称空间是否正常运行(失败)。
  2. 尝试不使用名称空间来动态加载模块(FAIL)。

代码:

// @/store/modules/User.ts
import { Module, VuexModule, Mutation, Action } from "vuex-module-decorators";
import { firebase, auth, GoogleProvider, StoreDB } from "@/services/fireinit";
import { IUser } from "~/types/user";
import { store } from "..";

// It seems like the "store" is messed somehow
@Module({ dynamic: true, namespaced: true, store: store, name: "user" })
export default class User extends VuexModule {
  user: IUser = null;

  @Action
  async autoSignIn(user: IUser) {
    this.context.commit("setUser", user);
  }

  @Action
  async signInWithGoogle() {
    return new Promise(resolve => {
      console.log("signInWithGoogle:", this);
      auth.signInWithRedirect(GoogleProvider);
      resolve();
    });
  }

  // trunked ...
}
// @/store/index.ts
import Vuex, { Store } from "vuex";
import User from "./modules/User";

// trunked ...

// Declare empty store first
export const store = new Vuex.Store<IStoreType>({});

const createStore = () => store;

export default createStore;
// @/pages/login.vue

// trunked ...

const User = namespace('user'); // [vuex] module namespace not found in mapActions(): user/

@Component({})
export default class LoginComponent extends Vue {
  @User.State("activeUser") stateUser;
  @User.Action("signInWithGoogle") actionSignInWithGoogle;
}

树:

├── pages
│   └── login.vue
├── store
│   ├── index.ts
│   └── modules
│       └── User.ts

我希望能够动态加载命名空间模块...

我尝试了所有可以在万维网上找到的内容,但我无法使其正常工作。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

好吧,我找到了一种可行的方法...

代码:

// @/store/index.ts
import Vuex, { Store } from "vuex";
import User from "./modules/User";

// trunked ...

// Declare empty store first
export const store = new Vuex.Store<IStoreType>({});
// No more "createStore" shit.
// @/pages/login.vue

// trunked ...

const User = namespace('modules/User/'); // "modules/" is important to make it work.

@Component({})
export default class LoginComponent extends Vue {
  @User.State("activeUser") stateUser;
  @User.Action("signInWithGoogle") actionSignInWithGoogle;
}