javascript - router.js 中的 Vue js "store is not defined"

标签 javascript vue.js vuex vue-router

我想执行重定向,但在重定向发生之前我想使用 store.dispatch 执行操作。控制台中出现错误“商店未定义”。

我尝试将整行代码放入一个变量中,并检查是否 true 和 null,错误消失,但操作从未被调用,并且调试器显示 vue 正在跳过 if 语句。


import Vue from 'vue'
import store from './store/index'
import Router from 'vue-router'
import Settings from './views/Settings.vue'


Vue.use(Router)

export default new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [
        {
            path: '/myPath',
            name: 'myPathName',
            component: {},
            beforeEnter(to, from, next) {
                //STORE is not defined
                store.dispatch("path/MY_ACTION");
                    next({
                        name: "destinationPath", 
                    })
            }
        }
-----------------------------------------------------------------------------
MY STORE
// STORE -> MODULES -> CONFIGURATION -> INDEX
import windowsModule from "../windows/index"
import mainDoorModule from "../maindoor/index"
import doorLeavesModule from "../doorleaves/index"
import doorModule from "../door/index"
import actions from "./actions"
import mutations from "./mutations"

export default {
    namespaced: true,
    modules: {
        windows: windowsModule,
        mainDoor: mainDoorModule,
        doorLeaves: doorLeavesModule,
        door: doorModule
    },
    state: {
        configurationId: 0,
        savedConfigurationsViewModel: [],
        errors: {},
        configurationsToSend: []
    },
    mutations,
    actions
}

//THE ACTION I AM TRYING TO REACH INSIDE ACTIONS
// STORE -> MODULES -> CONFIGURATION -> ACTIONS 

GET_DEFAULT_CONFIGURATION({ commit }) {
    commit('SET_CONFIGURATION', {
        //DATA
    }
}

最佳答案

您需要将其安装在主组件中。然后你通过this.$store引用它。阅读 Vuex 文档。

export const store = new Vuex.Store({
  state: {},
  mutations: {},
  getters: {}
})
import store from './store/index'

new Vue({
  store, // <- here
  el: '#app'
})

关于javascript - router.js 中的 Vue js "store is not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59249378/

相关文章:

authentication - 关闭选项卡后,Vuex 持久状态不会删除状态

javascript - 如何使用 chrome 扩展插入 HTML?

javascript - 我在 javascript 中读取 cookie 时遇到问题

javascript - append Facebook 评论

css - Vuetify v-card 适合拼图(砌体布局)

javascript - 如何动态更改 Vue.js 转换

javascript - 在 Bootstrap Vue 中动态更改选择输入

javascript - vuejs/vuex 中的观察和突变

vue.js - VUEX 没有找到 mutation 但它被定义了

javascript - 清理稍后将出现在 HTML 中的输入