Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

392
Visualizações
Vue Router - Same path, different components

In Vue Router I want to show different components in the same path, based on a variable.

If isFirstTimeAccess is true i want to show Welcome Component, if it is false I want to show AuthHandler component in the same path on the browser -> /my-sub-url/

Example:

export const router = createRouter({
    history: createWebHistory(),
    routes: [{
            path: '/my-sub-url/',
            component: welcome,
            beforeEnter(to, from, next) {
                if (store.getters.isFirstTimeAccess) {
                    next();
                } else {
                    next({
                        name: 'auth'
                    });
                }
            },
            children: [{
                    name: 'auth',
                    path: '',
                    component: AuthHandler,
                },
            ]
        }
    ];
});

but If i tried this code if I set :

  • isFirstTimeAccess === true -> it works and show the welcome component

  • isFirstTimeAccess === false -> it show me this error in browser:

[Vue Router warn]: Detected an infinite redirection in a navigation guard when going from "/" to "/my-sub-url/". Aborting to avoid a Stack Overflow. This will break in production if not fixed. vue-router.esm-bundler.js:72

[Vue Router warn]: Unexpected error when starting the router: Error: Infinite redirect in navigation guard

Can someone help me please?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try this way

<template>
  <component :is="checkComponentAccess"></component>
</template>

<script>

export default {
  components: {
    WelcomeComponent: () => import("/path/to/component/WelcomeComponent"),
    SecondTimeComponent: () => import("/path/to/component/SecondTimeComponent")
  },
  computed: {
    ...mapGetters("user", ["isFirstTimeAccess"]),
    checkComponentAccess() {
      return this.isFirstTimeAccess === "citizen" ? "FirstComponent" : "SecondComponent";
    }
  }
};
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

This seems to be a perfect case for Vue Router's navigation guards. They even have a specific example for an authentication case like yours.

Just copied from there:

router.beforeEach(async (to, from) => {
  if (
    // make sure the user is authenticated
    !isAuthenticated &&
    // ❗️ Avoid an infinite redirect
    to.name !== 'Login'
  ) {
    // redirect the user to the login page
    return { name: 'Login' }
  }
})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda