Search code examples
vue.jsvuexnuxt.jsvuex-modules

Dynamic & Namespaced modules not registered (vuex-module-decorators, vuex-class)


The module is not registered:

$nuxt.$store._modulesNamespaceMap // No properties

Also, i'm getting this warning:

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

What i've tried:

  1. loading manually the module to see if the namespace was working (FAIL).
  2. Try without namespace to load dynamically the module (FAIL).

Code:

// @/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;
}

Tree:

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

I expect to be able to load dynamically & namespaced modules...

I tried everything i could find on the world wide web but i can't manage to make it work.

What i'm doing wrong ?


Solution

  • Ok, i found a way that work...

    Code:

    // @/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;
    }