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

171
Vistas
Rewrite Vue component to be more DRY

I am just looking for some advice on if there is a more efficient way to write the below, make it a little more DRY?

    <h2>{{ title }}</h2>
    <p>{{ subtitle }}</p>

As I am making the same check for this.name on both title and subtitle I thought there could be a between way than my current implementation, any ideas?

  computed: {
    title() {
      if (this.name === 'discounts_offers') {
        return this.$t('discounts.title')
      }
      if (this.name === 'newsletter') {
        return this.$t('newsletters.title')
      }
      if (this.name === 'product_upgrade') {
        return this.$t('upgrade.title')
      }
      return this.name
   },
    subtitle() {
      if (this.name === 'discounts_offers') {
        return this.$t('discounts.subtitle')
      }
      if (this.name === 'newsletter') {
        return this.$t('newsletters.subtitle')
      }
      if (this.name === 'product_upgrade') {
        return this.$t('upgrade.subtitle')
      }
      return this.name
   },
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

My suggestion :

Literal keys should be same as the value of this.name. So that you can translate them dynamically.

Hence, Instead of multiple if/else clause. You can directly achieve that with a single line of code.

title() {
  return this.$t(this.name)
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

How about something like this?

  data() {
    return {
      map: {
        discounts_offers: "discounts",
        newsletter: "newsletters",
        product_upgrade: "upgrade"
      }
    }
  },
  computed: {
    title() {
      return this.map[this.name] ? this.$t(`${this.map[this.name]}.title`) : this.name
    },
    subtitle() {
      return this.map[this.name] ? this.$t(`${this.map[this.name]}.subtitle`) : this.name
    },
  }

about 4 years ago · Juan Pablo Isaza 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