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

266
Visualizações
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 Respostas
Responde à pergunta

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 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