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

184
Visualizações
Which approach of sharing template and logic is preferred in VUE?

I have two unrelated context components which share logic and template in a way that looks like one is extending the other.

Imagine Drop and Pick components like this:

// pick.js
import Vue from 'vue'
import Component from 'vue-class-component'

@Component
export default class Pick extends Vue {
  template: '<p>{{name}}</p>',
  created() {
    console.log('Hello Pick');
  }
  data: { 
    name: 'pick',
    count: 0
  },
  methods: {
    add: function () {
      this.count++;
    }
  }
}

// drop.js
import Vue from 'vue'
import Component from 'vue-class-component'

@Component
export default class Drop extends Vue {
  template: '<p>{{name}}</p> <span v-if="isUserFriendly">Greetings!!!</span>',
  created() {
    console.log('Hello Drop');
  }
  data: { 
    name: 'drop',
    isUserFriendly: false,
    count: 0,
    drops: 0
  },
  methods: {
    add: function () {
      this.count++;
      this.drops++;
    },
    remove: function () {
      this.count--;
    },
  }
}

Intuitively, one can say Drop is extending Pick while adding some functionality and alter the template. This seems to be a good solution but then I thought to myself that to achieve that Pick template should look like this:

<p>{{name}}</p> 
<span v-if="isUserFriendly">Greetings!!!</span>

Remember that I said unrelated context components? Now Pick needs to know about isUserFriendly which is unacceptable nor I think its the right solution. Moreover it will create overhead in the future as inheritance can be hard to maintain (imagine more components to extend and logic to share).

What do you think will be the best approach here? Maybe there is another way around which Im not familiar with?

Thanks for the help!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Component composition is a common way to do this. Drop component contains a superset of functionality, i.e. pick component should allow to be wrapped in a way that will allow for desired output and behaviour.

For parent-child relationship, data pieces are passed as props and events, and view pieces are passed as slots.

Additional logic stays in parent component.

In child component (Pick):

<div>
  <p>{{name}}</p>
  <slot name="extraText"/>
</div>

and

add: function () {
  this.count++;
  this.$emit('add');
}

In parent component (Drop):

<Pick @add="drops++">
  <template v-slot:extraText>
    <span>Greetings!!!</span>
  </template>
</Pick>
over 4 years ago · Santiago Trujillo 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