B
B
ByJumping2021-12-15 17:04:10
Vue.js
ByJumping, 2021-12-15 17:04:10

How to disable routing by condition in vue/nuxt?

Project on vue 2

export function createRouter() {
  return new Router({
    mode: 'history',
    routes: [
      {
        path: '/',
        component: Index,
        name: 'index',
        beforeEnter: () => {},
      },
      {
        path: '/dashboard',
        component: Dashboard,
        name: 'dashboard',
      },
      {
        path: '/reconstruction',
        component: Reconstruction,
        name: 'reconstruction',
      },
    ],
  });
}


A true/false flag will arrive from the backend, and depending on this, I either show the application, that is, the default/index component, or if technical work is underway, I should show only 1 component - reconstruction and prohibit the transition to other components. I don’t understand where I need to forward this condition, while I want to do something like if (false) - path: '/reconstruction', and even if a person enters /login on the way, he will not go anywhere

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aetae, 2021-12-15
@Aetae

beforeEach , or populate all routes dynamically via addRoute .

B
ByJumping, 2021-12-15
@ByJumping

do

.beforeEach((to, from, next) => {
const flag = 42;
if (flag !== 42) {
  next('reconstruction');
} else {
  next();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question