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

155
Visualizações
Refactoring vue component with inconsistent props

I want to refactor the Vue component where the same data comes in different structures.

Example Appointment.vue component:

<template>
  <div>
    <div v-if="config.data.user.user_id">
      {{ config.data.user.user_id }}
    </div>
    <div v-if="config.data.user.address.full_address">
      {{ config.data.user.address.full_address }}
    </div>
  .
  .
  .
  </div>
</template>

<script>
  export default {
    name: 'appointment',
    props: {
      config: {
        required: true,
      },
    },
    // Some operations uses 'config' prop
    //.
    //.
    //.
  }
</script>

In CreateAppointment.vue component that uses Apponintment.vue:

<template>
  <div>
    <appointment :config="data"/>
  .
  .
  .
  </div>
</template>

<script>
  export default {
    name: 'Create Appointment',
    data() {
      return {
        data: null,
      };
    },
    methods: {
      openAppointment() {
        const result = // api get operation that returns object which I'll use on Appointment.vue
        if (result) {
          const temp = {
            user: null,
          }
          temp.appointment_id = result.appointment_id;
          .
          .
          .
          temp.user.user_id = result.user_profile.user_id;
          .
          .
          .
          this.data = { ...temp };
        }
      }
    }
  }
</script>

In AppointmentList.vue which uses Appointment.vue:

<template>
  <div>
    <appointment :config="data"/>
  .
  .
  .
  </div>
</template>

<script>
  export default {
    name: 'AppointmentList',
    data() {
      return {
        data: null,
      };
    },
    methods: {
      openAppointment() {
        const result = // api get operation that returns object which I'll use on Appointment.vue
        if (result) {
          const temp = {
            user: null,
          }
          temp.appointment_id = result.appointment_id;
          .
          .
          .
          temp.user.user_id = result.user_id;
          temp.user.name = result.user_name;
          .
          .
          .
          this.data = { ...temp };
        }
      }
    }
  }
</script>

As you can see, in CreateAppointment.vue Api returns user_id in user_profile object. But in ListAppointment.vue, Api returns data in another structure. In this case, wherever Appointment.vue is used, I need to transform the incoming data into the shape Appointment.vue wants. How should I go about avoiding this? (It is not possible for me to change the data returned from the API.) I thought of using mixin or Wrapper Component. What kind of solution would make sense to implement in this and similar cases?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

If you can't change the API response scheme, the only way is to mutate data according to your needs :( If you have to do the same with user more than in one place, I would suggest adding a helper function or a mixin, otherwise just leave as is and extract the logic when it becomes necessary. By the way, why do you have to fetch the same data multiple times, or it's not the same? You can just emit event from the child component to the parent, and handle the API call from there. This way you will always have consistent data...

about 4 years ago · Santiago Gelvez 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