简体   繁体   English

Nuxt,将Vuex存储拆分为单独的文件会产生错误:未知突变类型:登录

[英]Nuxt, splitting up Vuex store into separate files gives error: unknown mutation type: login

I'm trying to split up my Nuxt Vuex store files into separate files . 我正在尝试将Nuxt Vuex存储文件拆分为单独的文件 And NOT have all Vuex getters , mutations and actions into one huge file. 并没有将所有Vuex gettersmutationsactions整合到一个大文件中。 This demo project is on Github by the way. 顺便说一下,这个演示项目在Github上

I'v read this official Nuxt Vuex Store documentation ; 我已经阅读了Nuxt Vuex官方商店文档 ; but can't seem to get it working. 但似乎无法使其正常工作。 It's a bit vague on where to put stuff. 在哪里放东西有点模糊。

I have the following in these files: 这些文件中包含以下内容:

Below is my: store/index.js 下面是我的:store / index.js

import Vue from "vue";
import Vuex from "vuex";
import Auth from "./modules/auth";

Vue.use(Vuex);

export const store = () => {
    return new Vuex.Store({
        state: {

        },
        modules: {
            Auth
        }
    })
}

This is in my: store/auth.js 这是在我的:store / auth.js

const state = () => {
    username: null
};

const getters = {
    username: state => {
        return state.username;
    },
    isAuthenticated: state => {
        return state.username != null;
    }
};

const mutations = {
    login: (vuexContext, username) => {
        vuexContext.username = username;
        this.$router.push("/dashboard");
    },
    logout: vuexContext => {
        vuexContext.username = null;
        this.$router.push("/");
    }
};

const actions = {

};

export default {
    state,
    getters,
    mutations,
    actions,
};

And finally in my: pages/index.vue 最后是我的:pages / index.vue

This is where I'm calling that login mutation: 这就是我所说的登录突变的地方:

<script>
    export default {
        layout: "non-logged-in",
        data() {
            return {
                username: null
            }
        },
        methods: {
            onSubmit() {
                this.$store.commit("login", this.username);
            }
        }
    }
</script>

The error I'm getting: 我得到的错误:

[vuex] unknown mutation type: login

What am I doing wrong here? 我在这里做错了什么? I thought i'm importing all the stuff correctly in the store/index.js 我以为我将所有东西正确地导入到store/index.js

You have to export your store constant like this inside your store/index.js file: 您必须这样在store / index.js文件中导出商店常量:

export default store

Put this code line at the end of your file. 将此代码行放在文件末尾。

So as @jeremy.raza described this is what I changed in order to get it working: 因此,正如@ jeremy.raza所描述的,这是我为了使其能够正常运行而进行的更改:

store/index.js 商店/index.js

import Vue from "vue";
import Vuex from "vuex";
import Auth from "./modules/auth";

Vue.use(Vuex)

const store = () => {
    return new Vuex.Store({
        state: {

        },
        modules: {
            Auth
        }
    })
}

export default store;

Changes in the store/auth.js store / auth.js中的更改

Note the changes in how I wrote the state , getters and mutations method notation. 注意我编写stategettersmutations方法符号的方式的变化。

const state = () => ({
    username: null
});

const getters = {
    username(state) {
        return state.username;
    },
    isAuthenticated(state) {
        return state.username != null;
    }
};

const mutations = {
    login(vuexContext, username) {
        vuexContext.username = username;
        this.$router.push("/dashboard");
    },
    logout(vuexContext) {
        vuexContext.username = null;
        this.$router.push("/");
    }
};

const actions = {

};

export default {
    state,
    getters,
    mutations,
    actions,
};

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

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