What is the best method for sharing templates and logic in VUE?

Two separate components with shared logic and template, making it appear as though one is extending the other.

Think of Drop and Pick components in this manner:

// 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--;
    },
  }
}

Initially, it may seem like Drop is extending Pick while adding extra functionality and modifying the template. However, forcing Pick to include information about isUserFriendly goes against the concept of unrelated context components.

This approach could lead to maintenance challenges down the line if more components are extended and shared logic increases.

Do you have any suggestions for a better solution? Is there another method that I am not familiar with?

Thank you for your input!

Answer №1

Utilizing component composition is a widely used method for achieving this goal. The Drop component consists of a comprehensive set of functionalities, whereas the Pick component can be wrapped in a manner that enables the desired output and behavior.

When establishing a parent-child relationship, data components are transmitted through props and events, while view components are transmitted through slots.

The parent component retains any additional logic.

Within the child component (Pick):

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

Additionally,

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

In the parent component (Drop):

<Pick @add="drops++">
  <template v-slot:extraText>
    <span>Greetings!!!</span>
  </template>
</Pick>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Categorize messages based on the date they were last read in Angular

I am looking to organize my chat application messages by date, similar to the layout in Microsoft Teams app. Here is an example of the message data: [ { "id": 577, "source": { "userID": 56469, ...

Integrating Vimeo videos into Angular applications

I am attempting to stream videos using URLs in my Angular application. Every time I try, I encounter the following error: Access to XMLHttpRequest at 'https://player.vimeo.com/video/548582212?badge=0&amp;autopause=0&amp;player_id=0&amp;ap ...

"Utilizing Angular's dynamic variable feature to apply ngClass dynamically

Looking for guidance on Angular - color change on button click. The loop binding is functioning well with dynamic variable display in an outer element like {{'profile3.q2_' + (i+1) | translate}}, but facing issues with [ngClass] variable binding ...

How can I remove the ID of the object data from the list?

When sending the data initially, there are no issues. However, when attempting to update, I am able to delete the root object's id but struggling to delete the ids of objects in the list. Any suggestions on what I can do? const handleSubmit = async ...

Experience Markdown's Single Input Update Feature

I have recently developed a Markdown editor using Vue, similar to the examples found on Vue's website. However, my query is more related to implementation rather than Vue itself. Therefore, I am open to suggestions that do not necessarily involve Vue. ...

Is it possible to remove a specific directory from the webpack build configuration in a vue-cli-3 setup?

After spending 3 hours adding the line: exclude: ['./src/assets/sass'] in 20 different places, I am seeking guidance on where it should actually be placed. Below is my current setup for the css-loader (util.js): 'use strict' const path ...

Nativescript encountered an error regarding faker: module './address' not found

Currently in the process of learning nativescript, I am experimenting with using faker to generate some data while working with typescript. Here are the versions I am using: Node - 6.9.4 Faker - 3.1.0 Typescript - 2.1.4 Encountered an error which i ...

A step-by-step guide on utilizing NuxtLink to successfully pass data between pages

Having an issue with Nuxt Link: -Initially, I use a v-for to generate some HTML <li v-for="store in stores"> <p>{{store.companyName}}</p> <p> {{store.url}} </p> <NuxtLink :to="nextPage">C ...

Error: Idle provider not found in the promise

Currently, I am integrating ng2-idle into an AngularJS 2 application. After successfully including the ng2-idle package in the node_modules directory of my project, I attempted to import it into one of my components as shown below: Dashboard.component.ts: ...

Troubleshooting: Issues with APIGateway's Default Integration

I'm currently utilizing the AWS CDK to construct my API Gateway REST API My objective is to have my RestApi configured to automatically return an HTTP 404 error, so I set it up as follows: this.gateway = new apigw.RestApi(this, "Gateway", { ...

"What is the methodology for specifying generics in a TypeScript FC component?"

How do you specify the type to pass to an Interface Props generic? (The Cat must be of type FC) interface CatProps<T> { value: T } const Cat: FC<CatProps<T>> = () => { return <h1>Hello World!</h1> } const cat = <Ca ...

Unveiling the method to fetch the emitted value from a child component and incorporate it into the parent component's return data in Vue

How can I retrieve the title value passed by the Topbar component and incorporate it into the data() return section? I attempted to create a method to pass the value, but was unsuccessful despite being able to successfully log the value in the parent file. ...

Passing image source from parent component to child component in Vue.js

I encountered an issue where I stored the image file name in a variable within the parent component and passed it to the child component using props. However, despite this setup, the child element is not displaying the image as expected. Here is the data ...

Issue TS2322 presents itself when attempting to assign a value of type 'string, number, or Date' to a variable of type 'Date' after upgrading to Angular 16

I recently upgraded my project from Angular 11 to Angular 16 and encountered an issue with the DTO models generated using the NPM package "ng-swagger-gen" from the Swagger JSON file of the Web API. In my C# class on the Web API side, I have a DateTime fiel ...

Cannot execute npm packages installed globally on Windows 10 machine

After installing typescript and nodemon on my Windows 10 machine using the typical npm install -g [package-name] command, I encountered a problem. When attempting to run them through the terminal, an application selector window would open prompting me to c ...

Capturing the returned value of a mutation in another mutation in Vuex

Having trouble accessing a getter from an action, and receiving the error message: getters.isCardLiked is not a function store/index.js export const getters = { isCardLiked(state, id) { const found = state.likedCards.find(likedCard => liked ...

Vue Router generates a URL containing %2F when dealing with slug routes

I am facing an issue in my Vue 3 application where I need to create a route for dynamic slugs. Currently, when using RouterLink, the generated URL contains %2F instead of actual slashes which is not the desired result. For example, the current URL looks l ...

Ways to assign the output of a function to a variable in JavaScript

I am looking to update the value of my function to a variable. As an example, I have the following: <input v-model="search.steering" @input="onChange('steering')"/> This means that I want to insert the steering every time I input text. ...

What is the best way to only buffer specific items from an observable source and emit the rest immediately?

In this scenario, I have a stream of numbers being emitted every second. My goal is to group these numbers into arrays for a duration of 4 seconds, except when the number emitted is divisible by 5, in which case I want it to be emitted immediately without ...

Tips for displaying dynamic content in VueJS?

I'm currently working on an app that allows users to choose which type of vuetify element they want to display on the page. There are 4 options available for selection. My goal is to render the corresponding vuetify component when a user clicks on the ...