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

62
Visualizações
Show me a badge in the navbar under certain VueJS pages

I am building a SPA with Vue 3 and now I have the case that I would like to show a badge in the navbar of certain pages. My structure:

// App.vue
<template>
  <NavBar />
  <RouterView />
</template>

My paths:

  • "/"
  • "/faq"
  • "counter/x" => here i need the badge in the navbar
  • "counter/y" => here i need the badge in the navbar

I also use a simple store solution. Here I have a function

export const store = reactive({
  isCounterPage: false,

  checkIsCounterPage() {
   this.isCounterPage = window.location.pathname.includes("/counter/");
  }
}

In the router/index.js File, I call the checkIsCounterPage() method.

// router/index.js
router.beforeEach(async (to, from, next) => {
  store.checkIsCounterPage()
}

And in my navbar, I use a condition where i check the variable:

// someView.vue
<template>
  <div v-if="store.isCounterPage" class="badge">BADGE</div> 
</template>

Unfortunately, it doesn't work the way I want it to. Only after a hard refresh of the page (for example F5 / refresh Page) it recognises where it is. So the problem is that the store method is only called once on initialisation and not on route change as I assume.

Question:

What am I doing wrong and is this the right way at all?

almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can add meta field to your counter routes:

routes: [
  { path: '/counter/x', component: x, meta: { isCounterPage: true } },
  ...
],

and check meta:

router.beforeEach(async (to, from, next) => {
  if (to.meta.isCounterPage) store.checkIsCounterPage(true)
  next()
}

and in store:

export const store = reactive({
  isCounterPage: false,

  checkIsCounterPage(val = false) {
    this.isCounterPage = val
  }
}
almost 4 years ago · Santiago Trujillo 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