Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

65
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda