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

267
Vistas
Vue component to change text based on data value

This is the data I get and I what to be able to replace the data content with readable text:

From parent:

 data: [
    { name: 'discounts_offers', type: 'EMAIL', consent: true },
    { name: 'newsletter', type: 'EMAIL', consent: true },
    { name: 'product_upgrade', type: 'EMAIL', consent: true },
    { name: 'sms_offer', type: 'SMS', consent: true },
    { name: 'post_offer', type: 'POST', consent: true }
  ]

This is my included component

  <CommunicationPreference
    v-for="(communication, index) in data"
    :key="index"
    :communication="communication"
  />

Then the communicationPreference.vue:

<template>
  <section>
    {{ communication.name }} //This should be Newsletters etc
  </section>
</template>

<script>
export default {
  props: {
    communication: {
      type: Object,
      default: null,
    },
  },
  data() {
    return {}
  },
  computed: {},
}
</script>

Then what I would like to do is if {{ communication.name }} equals 'discounts_offers' to use the text "Discounts and offers" like the images attached. Any solutions for the best approach to this?

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use computed property for scenario like this. Something like this.

App.vue

<template>
  <CommunicationPreference
    v-for="(communication, index) in preferences"
    :key="index"
    :type="communication.type"
    :name="communication.name"
    v-model:consent="preferences[index].consent"
  />
</template>

<script>
import CommunicationPreference from "./components/CommunicationPreference.vue";

export default {
  name: "App",
  components: {
    CommunicationPreference,
  },
  data() {
    return {
      preferences: [
        { name: "discounts_offers", type: "EMAIL", consent: true },
        { name: "newsletter", type: "EMAIL", consent: true },
        { name: "product_upgrade", type: "EMAIL", consent: true },
        { name: "sms_offer", type: "SMS", consent: true },
        { name: "post_offer", type: "POST", consent: true },
      ],
    };
  },
};
</script>

and in CommunicationPreference.vue

<template>
  <div>
    <label
      ><input
        type="checkbox"
        name="preference"
        :value="consent"
        :checked="consent === true"
        @change="$emit('update:consent', $event.target.checked)"
      />{{ label }}</label
    >
  </div>
</template>

<script>
export default {
  name: "CommunicationPreference",
  props: {
    type: String,
    name: String,
    consent: Boolean,
  },
  computed: {
    label() {
      if (this.name === "newsletter") {
        return "Newsletters";
      }

      if (this.name === "discounts_offers") {
        return "Discount and offers";
      }

      if (this.name === "product_upgrade") {
        return "Upgrade recommendations";
      }

      return this.name;
    },
  },
};
</script>

Something like that....not tested though.

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