vue.js - 无法读取 Vuex 中未定义的属性 'dispatch'

标签 vue.js vuex

我正在尝试对 vuex 存储中的“logOutUser”执行调度,我在响应中收到以下错误消息:

TypeError: Cannot read property 'dispatch' of undefined

deleteUser.vue(调度操作不起作用的组件):

<template>
    <v-dialog v-model="openDelete" persistent max-width="500px">
        <v-card>
            <v-card-title>
                <h4>Delete User</h4>
            </v-card-title> 
            <v-card-text>
                <h2>Are You Sure?</h2>
                <p>Deleting your user is a permanent action.</p>
                <br>
                <br>
                <v-btn
                 color="primary"
                 @click="deleteUser">
                 Delete User
                </v-btn>
                <v-btn
                 color="primary"
                 @click="openDelete = false">
                 Close
                </v-btn>  
            </v-card-text>  
        </v-card>   
    </v-dialog> 
</template>
<script>
import router from '../../router/index.js'
const firebase = require('../../firebaseConfig.js')
export default {
    data: function(){
        return {
            openDelete: true
        }
    },
    methods: {
        deleteUser: function(){
            let user = firebase.auth.currentUser
            const docId = user.uid
            console.log("Trying to delete user ", docId)
            user.delete().then(function() {
            }).catch(function(error) {
                console.error("Error deleting user: ", error)
            });
            firebase.firestore.collection("users").doc(docId).delete().then(() => {
                        console.log('Trying to Log Out in Vuex')
                        this.$store.dispatch('user/logOutUser')
                        alert("User Deleted Successfully!")
                }).catch(function(error) {
                    console.error("Error removing document: ", error);
                });
            router.push('hello')    
            this.openDelete = false
        }
    }
}
</script>

store.js:

import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
import genre from './modules/genre'

Vue.use(Vuex)

export default new Vuex.Store({
    modules: {
        user,
        genre
    }
})

用户.js:

const firebase=require('../firebaseConfig.js')

const state = {
    currentUser: null,
    userProfile: {}
}

const actions = {
        fetchUserProfile({commit, state}, uid){
            firebase.firestore.collection("users").doc(uid).get().then((doc)=>{
                commit('setUserProfile', doc.data())
            }).catch((error)=>{
                console.error(error)
            })
        },
        logOutUser({commit, state}){
            commit('setCurrentUser', null)
            commit('setUserProfile', {})
            console.log('Logged Out In Vuex')
        }
}

const mutations = 
    {
        setCurrentUser(state, val){
            state.currentUser = val
        },
        setUserProfile(state, val){
            state.userProfile = val
        }
    }

export default {
    namespaced: true,
    state,
    actions,
    mutations
}   

编辑:这是我的 main.js 文件:

import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store.js'
const firebase = require('./firebaseConfig.js')

Vue.config.productionTip = false

let app

firebase.auth.onAuthStateChanged(user => {
  if(!app){
    /* eslint-disable no-new */
    app = new Vue({
      el: '#app',
      router,
      store,
      components: { App },
      template: '<App/>'
    })
  }
});

我应该提到我正在从我的应用程序中的另一个组件分派(dispatch)此操作,并且它从那里工作得非常好。

谢谢!

最佳答案

那是因为您可能没有将 store 选项添加到 Vue 的根实例。通过提供它,您将能够从 root 的所有子组件访问存储。因此,您的根实例应如下所示:

import store from './store'

const app = new Vue({
  /* .. other properties .. */
  store
})

现在您可以在组件中自由使用 this.$store

关于vue.js - 无法读取 Vuex 中未定义的属性 'dispatch',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52482814/

相关文章:

javascript - 即使元素不在页面上,也会调用 VueJS 实例

vue.js - 将表单提交推迟到方法?

vue.js - Nuxt/Vue react 体属性

javascript - Vue,获取返回空数组

javascript - Vue : how to update cart total price when user increment item in child component

vuejs2 - 在 Nuxt.js 中 CORS 阻塞客户端请求

vue.js 和侧边栏示例

debugging - VS Code - 在 Laravel 项目中调试 VUE 组件 - 未验证的断点

laravel - 当索引未知时更新特定数组条目

javascript - Nuxt,将 Vuex 存储拆分为单独的文件会出现错误 : unknown mutation type: login