Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!