gpt4 book ai didi

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

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

我想执行重定向,但在重定向发生之前我想使用 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/

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