Answer the question
In order to leave comments, you need to log in
Why is vuex $store throwing "undefined"?
Hai. I resumed training Vue.JS, and then there was a problem:
There is a route file in which all routes and authorization checks are registered:
import Vue from 'vue'
import VueRouter from 'vue-router'
import Meta from 'vue-meta'
Vue.use(VueRouter)
Vue.use(Meta)
/**
* Для пользователей
* @constructor
*/
const Home = () => import('../components/home.vue')
/**
* Для гостей
* @constructor
*/
const Welcome = () => import('../components/welcome.vue')
/**
* Страница ошибок
* @constructor
*/
const NotFound = () => import('../components/errors/NotFound.vue')
const router = new VueRouter({
mode: 'history',
routes: [
/* Нейтральные страницы */
{ path: '/', name: 'home', component: Home, meta: {isAuth: true} },
{ path: '/welcome', name: 'welcome', component: Welcome, meta: {guest: true} },
{
/* 404 - Page not found */
path: '/404',
name: 'notfound',
component: NotFound
}, {
path: '*',
redirect: '/404'
}
],
linkActiveClass: '',
linkExactActiveClass: ''
});
console.log(this.$store) // Пробуем получить хоть что-то
// Тут будет проверка на авторизацию и выдачу необходимого роута
// router.beforeEach(
// (to, from, next) => {
// if(to.matched.some(record => record.meta.isGuest)){
// if(this.$store.state.auth.check){
// next({
// path: '/'
// })
// } else next()
// }else if(to.matched.some(record => record.meta.isAuth)){
// if(!this.$store.state.auth.check){
// next({
// path: '/auth/login'
// })
// }else{
// next()
// }
// } else next()
// }
// )
export default router
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const state = {
auth: {
check: false,
user: {}
}
}
export default new Vuex.Store({
strict: process.env.NODE_ENV !== 'production',
state,
getters: {
/**
* Проверка пользователя на авторизацию
* @param state
* @returns {boolean}
*/
authCheck: (state) => {
if(state.auth.check){
return true
}
return false
}
},
mutations: {},
})
Vue.store
Vue.$store
this.store
this.$store
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question