0

I want to perform a redirect, but before the redirect happens i want to perform a action using store.dispatch. Error is "store is not defined" in the console.

I have tried putting the whole line of code in a variable and check if true and if null, the error dissapears but the actions never gets called, and the debugger shows vue is jumping over the if-statement.


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
    }
}

12
  • Try this.$store.dispatch Commented Dec 9, 2019 at 13:03
  • Thanks for the answer! Still undefined. Commented Dec 9, 2019 at 13:05
  • Update your code with a larger fragment, because if it is not a component but a main module, it may mean that you have installed it incorrectly. Commented Dec 9, 2019 at 13:06
  • show your store Commented Dec 9, 2019 at 13:14
  • @Gander so i've added to show some imports, but i dont really know what would make sense to show. Thanks for the answer! Commented Dec 9, 2019 at 13:14

2 Answers 2

1

You need to install it in the main component. Then you refer to this through this.$store. Read the Vuex documentation.

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

new Vue({
  store, // <- here
  el: '#app'
})
Sign up to request clarification or add additional context in comments.

Comments

0

So it resolved. It was a Chrome bug saying that store is undefined, it's in webpack. Store was defined. The problem was the mismatch between actions and mutations sending payloads that did not match types.

Thanks for all the answers, i appreciate it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.