Vuex: this.$store.dispatch is not a function(Vuex:this.$Store.Dispatch不是一个函数)
本文介绍了Vuex:this.$Store.Dispatch不是一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Vuex从FireBase获取数据。我正在与Firebase通信,将数据提交给突变,Getter将从州获得数据。然后我想在App.vue中显示状态数据。
在App.vue组件中,我在挂载函数中编写了调度,并尝试从Vuex获取数据。但是,我得到错误"TypeError: this.$store.dispatch is not a function"
,当我检查状态时,它显示为未定义。我的动作和突变写得正确吗?为什么我会收到该错误?
store.js
import Vue from 'vue';
import Vuex from 'vuex';
import firebase from 'firebase';
Vue.use(Vuex);
export default new Vuex.mapState({
state: {
notebooks: [],
},
getters: {
getItems: (state) => {
return state.notebooks;
},
},
mutations: {
setItems: function(state, notebooks) {
state.notebooks = notebooks;
},
},
actions: {
async getTodos({ commit }) {
var notebooksRef = await firebase.firestore().collection('notebooks');
const notebookList = [];
notebooksRef.onSnapshot((snap) => {
snap.forEach((doc) => {
let notebooks = {
id: doc.id,
notebook: doc.data(),
};
notebookList.push(notebooks);
});
commit('setItems', notebookList);
});
},
},
});
App.vue
<template>
<div id="app"></div>
</template>
<script>
export default {
name: 'App',
computed: {
notebooks: function() {
return this.$store.state.notebooks;
},
},
mounted() {
this.$store.dispatch('getTodos');
},
};
</script>
main.js
import Vue from 'vue';
import App from './App.vue';
import store from './store';
import './firebase';
Vue.config.productionTip = false;
new Vue({
store,
render: (h) => h(App),
}).$mount('#app');
firebase.js
import firebase from 'firebase';
const config = {
apiKey: '****',
authDomain: '****',
databaseURL: '****',
projectId: '****',
storageBucket: '****',
messagingSenderId: '****',
appId: '****',
};
// Initialize Firebase
firebase.initializeApp(config);
推荐答案
在您的store.js
文件中尝试export default new Vuex.Store({
。
这篇关于Vuex:this.$Store.Dispatch不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:Vuex:this.$Store.Dispatch不是一个函数


猜你喜欢
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01