Answer the question
In order to leave comments, you need to log in
How can you prevent a vue component from being rendered based on state in vuex?
There is a SPA application with access via api. There is, for example, a state in vuex : isUserLogged = true/false (whether the user is logged in to the site), or the necessary input parameters for the component.
What is the best way to check if the vue component is rendered to the user? This implementation seems a bit irrational.
<template v-if="isUserLogged">
...
</template>
Route::get('/{any}', '[email protected]')->where('any', '.*');
const router = new VueRouter({
mode: 'history',
routes,
});
const app = new Vue({
el: '#app',
store,
components: {MainApp},
router,
});
router.beforeEach((to, from, next) => {
if (to.matched.some(route => route.meta.requiresAuth) && (!store.state.isLoggedIn)) {
next({name: 'login'});
return
}
if (to.path === '/login' && store.state.isLoggedIn) {
next({name: 'dashboard'});
return
}
next()
});
const router = new VueRouter({
mode: 'history',
routes,
});
router.beforeEach((to, from, next) => {
if (to.matched.some(route => route.meta.requiresAuth) && (!store.state.isLoggedIn)) {
next({name: 'login'});
return
}
if (to.path === '/login' && store.state.isLoggedIn) {
next({name: 'dashboard'});
return
}
next()
});
const app = new Vue({
el: '#app',
store,
components: {MainApp},
router,
});
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