gpt4 book ai didi

javascript - Vuex 返回 "TypeError: Cannot read property ' getters' of undefined"

转载 作者:行者123 更新时间:2023-12-03 12:21:27 25 4
gpt4 key购买 nike

Vuex 似乎无法为我的 loading_state 找到我的 getter。

但是,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;
}
}
});

应用程序.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)

最佳答案

我相信正确的语法是

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

关于javascript - Vuex 返回 "TypeError: Cannot read property ' getters' of undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58651266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com