Vuex返回“ TypeError:无法读取未定义的属性'getters'”

时间:2019-10-31 21:17:46

标签: javascript vue.js vuex

Vuex似乎无法为我的loading_state找到我的吸气剂。

但是,vuex状态和getter仍在Vue开发人员工具中注册。我似乎无法在应用程序中调用它们。

我尝试过更改变量名称,通过不同的方法调用getter。

loading_store.js

import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export const loading_store = new Vuex.Store({
    state: {
        loading: true
    },
    mutations: {
        setFalse(state) {
            state.loading = false;
        },
        setTrue(state) {
            state.loading = true;
        }
    },
    getters: {
        isLoading: state => {
            return state.loading;
        }
    }
});

App.vue

<template>
    <div id="app">
        <Navigation />
        <loading-spinner v-if="isLoading" />
        <router-view class="m-3" />
    </div>
</template>

<script>
import Navigation from "@/components/Navigation";
import LoadingSpinner from "@/components/LoadingSpinner";
import { mapGetters } from "vuex";

export default {
    name: "app",
    components: {
        LoadingSpinner,
        Navigation
    },
    metaInfo: {
        title: "SnowStats"
    },
    computed: {
        ...mapGetters(["isLoading"])
    }
};
</script>

main.js

import Vue from "vue";
import App from "./App.vue";
//////////
import { loading_store } from "./state/loading_store";

/////////

new Vue({
    state: loading_store,
    router,
    render: h => h(App)
}).$mount("#app");

错误消息:

    vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in render: "TypeError: Cannot read property 'getters' of undefined"

    found in

    ---> <App> at src/App.vue
           <Root>


    TypeError: Cannot read property 'getters' of undefined
        at VueComponent.mappedGetter (vuex.esm.js?2f62:896)
        at Watcher.get (vue.runtime.esm.js?2b0e:4473)
        at Watcher.evaluate (vue.runtime.esm.js?2b0e:4578)
        at VueComponent.computedGetter [as isLoading] (vue.runtime.esm.js?2b0e:4830)
        at Object.get (vue.runtime.esm.js?2b0e:2072)
        at Proxy.render (eval at ./node_modules/cache-loader/dist/cjs.js?{"cacheDirectory":"node_modules/.cache/vue-loader","cacheIdentifier":"34f9cbf8-vue-loader-template"}!./node_modules/vue-loader/lib/loaders/templateLoader.js?!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/App.vue?vue&type=template&id=7ba5bd90& (app.js:947), <anonymous>:13:11)
        at VueComponent.Vue._render (vue.runtime.esm.js?2b0e:3542)
        at VueComponent.updateComponent (vue.runtime.esm.js?2b0e:4060)
        at Watcher.get (vue.runtime.esm.js?2b0e:4473)
        at new Watcher (vue.runtime.esm.js?2b0e:4462)

1 个答案:

答案 0 :(得分:1)

我相信正确的语法是

new Vue({
    store: loading_store,
    router,
    render: h => h(App)
}).$mount("#app");