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

159
Vistas
Other ways of showing prop-based conditonal text in Vue?

I have a component that renders text based on the users membership status and I want to change the interpolated text based on that prop value. Is there a more efficient way of showing different text based on the prop besides a bunch divs/p tags withv-if or v-show?

It's just a ton of text to constantly have a bunch of stacked divs.

Any advice would be appreciated!

Cheers!

<script lang="ts" setup>
import { PropType } from 'vue'

const props = defineProps({
  kind: {
    type: String as PropType<'subscribed' | 'unsubscribed'>,
    default: 'subscribed',
  },
  planId: {
    type: String as PropType<'standard' | 'silver' | 'gold' | 'platinum' | 'diamond' | 'no plan'>,
    default: 'standard',
  },
})
</script>

<template>
  <div class="c-promotion-plan-card" data-cy="component-promotion-plan-card">
    <div class="flex items-baseline mb-sm">
      <div v-if="planId === 'standard'" class="text-h6 text-dark">Standard Gang</div>
      <div v-if="planId === 'silver'" class="text-h6 text-dark">Silver Foxes</div>
      <div v-if="planId === 'gold'" class="text-h6 text-dark">Golden Girls</div>
      <div v-if="planId === 'platinum'" class="text-h6 text-dark">Platinum Boys</div>
      <div v-if="planId === 'diamond'" class="text-h6 text-dark">Diamond Dudes</div>
      <div v-if="planId === 'no plan'" class="text-h6 text-dark">
        No Plan Posse
      </div>
    </div>
  </div>
</template>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Yes, there is a better approach. You can define a computed property like Vue2 Syntax

computed: {
 getLabel() {
  // Assuming that 'planId' is the dependency prop
  if(this.planId === 'standard') return 'Standard Gang';
  else if(this.planId === 'silver') return 'Silver Foxes';
  ....
  return 'No Plan Posse' // For 'no plan' condition
 }

Vue3 Syntax

setup(props) {
 // 1.getLabel depends on firstName,lastName.
 const getLabel = computed(() => {
  // Assuming that 'planId' is the dependency prop
  if(props.planId === 'standard') return 'Standard Gang';
  else if(props.planId === 'silver') return 'Silver Foxes';
  ....
  return 'No Plan Posse' // For 'no plan' condition
 });
 return {
  getLabel,
 };
},

and then call this computed within interpolation inside you template like

<div class="text-h6 text-dark">{{getLabel}}</div>
about 4 years ago · Juan Pablo Isaza Denunciar

0

Maybe something like following snippet (map your plans and use computed property ):

const { ref, computed } = Vue
const app = Vue.createApp({
  props: {
    kind: {
      type: String,
      default: 'subscribed',
    },
    planId: {
      type: String,
      default: 'standard',
    },
  },
  setup(props) {
    const plans = ref([{plan:'standard', name: 'Standard Gang'}, {plan:'silver', name: 'Silver Foxes'}, {plan:'gold', name: 'Golden Girls'}, {plan:'platinum', name: 'Platinum Boys'}, {plan:'diamond', name: 'Diamond Dudes'}, {plan: 'no plan', name: 'No Plan Posse'}])
    const handlePlan = computed(() => plans.value.find(p => p.plan === props.planId))
    return { plans, handlePlan }
  },
})
app.mount('#demo')
.style-class {
  color: red;
  font-size: 2em;
}
.other-style {
  color: blue;
}
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="demo">
  <div class="c-promotion-plan-card" data-cy="component-promotion-plan-card">
    <div class="flex items-baseline mb-sm">
      <div class="text-h6 text-dark" :class="handlePlan.plan === 'standard' ? 'style-class' : 'other-style'">{{ handlePlan.name }}</div>
    </div>
  </div>
</div>

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