vuex-module-decorators: Error "[vuex] unknown action type:" in Nuxt, however I did all exactly according "Accessing modules with NuxtJS" documentation
Let me repeat: I did all according Accessing modules with NuxtJS explanations.
Store Module:
import { Action, Mutation, VuexModule, Module } from 'vuex-module-decorators';
@Module({
stateFactory: true,
name: "AUTHENTICATION__STORE_MODULE"
})
export default class AuthenticationStoreModule extends VuexModule {
private _token: string | null = null;
@Action
public async signIn(formData: { userId: string, password: string }): Promise<void> {
console.log("Called!");
const token: string = await new Promise(
(resolve: (token: string)=> void): void => {
setTimeout(() => { resolve('mock-token'); }, 2000);
});
console.log(token);
}
// ...
}
TypeScript types validation is all right. However, await authenticationStoreModule.signIn(formData)
does not work.
Component:
import { Vue, Component, Prop } from 'vue-property-decorator';
import VueReferencesInspector from '~/utils/VueReferencesInspector';
import { authenticationStoreModule } from "~/store/";
interface IElementUiForm extends Vue {
validate: (validationResult: (valid: boolean)=> void)=> void;
}
@Component
export default class extends Vue {
// ...
private onSubmit(): void {
// ...
try {
console.log('~~~');
console.log(authenticationStoreModule);
console.log(authenticationStoreModule.signIn(formData));
await authenticationStoreModule.signIn(formData);
this.$router.push('/admin');
} catch (error) {
this.dataIsBeingSubmittedNow = false;
}
});
}
}
console.log
s are;
As you see authenticationStoreModule
and authenticationStoreModule.signIn
are not undefined. However, console.log("Called!");
did not output, so action signIn
has not been called.
Once npm install
done,
- Execute
npm run dev
- go to page
http://localhost:3000/admin/login
- Try to login
You will see same console errors as in image.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 4
- Comments: 27 (9 by maintainers)
It took me some trial and error, and the documentation & examples don’t mention this at all, but it seems that you have to
export default
your module classes. See my minimal example that seems to work correctly.Try:
For Nuxt usage you should match the filename and namespace of your module, so this module should be in
~/store/auth.ts
.None of the solutions proposed here work for me either. I’ve recreated the issue in this repo:
https://github.com/qelix/nuxt-vuex-module-decorators-repro
Steps to reproduce:
yarn && yarn dev
localhost:3000
[vuex] unknown mutation type: test/setTest
in console as proof that it doesn’t workOr just use Codesandbox: https://codesandbox.io/s/github/qelix/nuxt-vuex-module-decorators-repro
It is a simple reproduction of the issue, containing only a minimal configuration. I did everything according to the docs and the comments in this issue. Still can’t get it to work, though.
Am I missing anything?
@webcoderkz Either move the module to
~/store/auth.ts
, move it out of the~/store
directory entirely, or pass it the namespacemodules/auth
.My code is here.
login.vue
utils/store-accessor.ts
@webcoderkz I believe the answer is there. In your case:
But I may be misunderstanding your question.
This might be the solution. For me exactly! Module name with namespace @/store/Vdp/main.ts