What distinctions can be made between extends and mixins in the context of the vue-class-component library?

Heads-up: Our Vue 2 setup is currently not utilizing the Composition API and we have no immediate plans to do so. This discussion focuses on .

Query: The guide for vue-class-components mentions that we can employ a "normal" extends to derive from a single parent component, or utilize the mixins helper function to inherit multiple mixins. Based on my understanding, a parent component is essentially a mixin (and vice versa), therefore I am curious whether the following code featuring a single parent component will produce identical child components:

import Vue from 'vue'
import Component from 'vue-class-component'

@Component
export class Parent extends Vue {
  p = 'P'
}

@Component
export class ChildOne extends Parent {
  created () {
    console.log(this.p)
  }
}

@Component
export class ChildTwo extends mixins(Parent) {
  created () {
    console.log(this.p)
  }
}

Answer №1

If you choose to go this route, the child components will both inherit the prop "p" from the Super class.

One major benefit of using mixins instead of extends is the ability to inherit from multiple classes. However, if you only need to inherit from a single parent class, then using mixins may not be necessary in my opinion.

Illustration

// customMixins.js
@Component
export class Star extends Vue {
  star = 'star'
}

@Component
export class Moon extends Vue {
  moon = 'moon'
}
import { Star, Moon } from './customMixins'

@Component
export class StarryNight extends mixins(Star, Moon) {
  mounted() {
    // now that it's mounted, let's enjoy the starry night
    makeAWish()
  }
  
  makeAWish() {
    console.log(`I wish upon a ${this.star} and ${this.moon}`)
  }
}

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

The outcome of data binding is the creation of a connected data object

I am attempting to link the rows' data to my view. Here is the backend code I am using: [Route("GetCarCount")] [HttpGet] public async Task<long> Count() { return await _context.Cars.CountAsync(); } ...

Vue 3: Utilizing the v-once directive within a for loop results in the display of

Am I making a mistake or is this a bug? It seems like a bug. The code below is displaying 2 divs with the same text: 'test1'. This is because of the 'v-once' directive. Each div should display a unique string instead. <script setup&g ...

Creating a lazy v-model binding for the value

My Vue component is a text editor from a 3rd party package, requiring a value to render correctly. <SomeComponent v-model:value="text" /> <script setup> const props = { records: { type: Object, }, } const text = compu ...

Unable to perform a post request with devise token authentication

I am facing an issue with making a post request to my controller endpoint. Interestingly, I was able to successfully create a user and retrieve all users using devise_token_auth. Following their documentation, I can perform CRUD operations on a user. Howe ...

What is the best way to retrieve all file names within the static folder in NuxtJs?

Currently, I am working with a frontend in NuxtJs and I have a requirement to display a list of all PDFs stored within the static folder. My goal is to create dynamic links that will allow users to open these documents in a separate browser window. There ...

Style will be applied to Angular2 when the object's ID exceeds 100

I am working with object markers that have different Id's assigned to them. Now, I am trying to apply additional styling when the id > 100. Check out the code snippet below: <span *ngIf="result.object.reference > 100" class="tooltip-data"&g ...

TypeScript error TS6053: Unable to locate file '.ts'

I encountered the following issue: Error TS6053: Could not find file 'xxx.ts'. Oddly enough, the file compiled without any issues yesterday. However, today it seems to be causing trouble. To troubleshoot, I ran a simple test: class HelloWorl ...

What is the process for importing components from a Stencil library into a React application?

After successfully creating: a stencilJS component library named "my-lib" using the "npm init stencil" wizard and an Ionic React app using "ionic start myApp tabs" I am now trying to incorporate the default "my-component," aka MyComponent from my-lib. H ...

The alias path for Vuetify Vue 3 has undergone modifications

Incorporated Vuetify into my Vue 3 project using 'vue add' command. Unfortunately, the following code snippet: <img src="@/assets/more-btn.svg" alt="" /> resulted in a 404 error, displaying the path localhost:8080/@/assets/more-btn.svg, i ...

The parameters provided for ionic2 do not align with any acceptable signature for the call target

Currently, I have 3 pages named adopt, adopt-design, and adopt-invite. To navigate between these pages, I am using navCtrl.push() to move forward and to go back to the previous page. Everything works smoothly on the browser, but when I try to build it for ...

What is the correct method for importing a Node module into Angular using TypeScript or AngularCLI?

As I integrate some "legacy" (non-typescript) JavaScript libraries into my Angular single page application. Usually, I simply include a CDN link in the index.html file like this: <script src="//cdnjs.cloudflare.com/ajax/libs/pako/1.0.6/pako.min.js"> ...

A guide on implementing the intl-tel-input plugin within an Angular 2+ project

Component : ng2-tel-input, Framework : Angular 4, JavaScript library : intl-tel-input Upon completing the installation with npm i ng2-tel-input I stumbled upon a note in the node_modules\intl-tel-input\src\js\intlTelInput.js file that ...

Guide on creating elements dynamically with the ngModel attribute in Ionic

I am currently working on dynamically creating an ion-input element when the user clicks on the "+" sign. My goal is to then insert all the input values into an array. While I have successfully created the inputs, I am facing an issue with assigning the [( ...

Check out the selected values in Ionic 3

I am trying to retrieve all the checked values from a checkbox list in an Ionic3 app when clicked. Below is the code snippet: <ion-content padding> <ion-list> <ion-item *ngFor="let item of items; let i= index"> <ion-label>{{i ...

Encountering a 404 error when trying to retrieve the Next.js 14 API route, despite specifying the use of route

Currently, I am working with Next.js version 14.2.3 and attempting to access the /api/tmp API route from the chat.tsx component. However, I keep encountering a 404 error even though I am using the route.ts file. What could be causing this issue? UPDATE: C ...

Typescript: Securing Data with the Crypto Module

I am currently working on encrypting a password using the built-in crypto module. Previously, I used createCipher which is now deprecated. I am wondering if there is still an effective way to achieve this. Here is the old code snippet: hashPassword(pass: ...

Vue CLI webpack causing image loading issues

What task am I currently working on? I am utilizing the intersection observer API to implement lazy loading functionality. What experiments have I conducted? I have tested the code on a basic HTML page and it functions flawlessly. However, when I incorpo ...

Encountering issues with npm installation on a Linux operating system

Embarking on a new journey learning Vue.js, I find myself in unchartered territory with Node.js. Initially, when I cloned the repository for my course and followed the setup instructions, everything ran smoothly without any issues. However, a blunder on my ...

Encountering the error message "Vue CLI service command is not recognized" while attempting to run a Vue application

When I run the following commands in the root directory of my Vue app (version 2.6.12) rm -rf node_modules npm install npm run serve An error occurs with the message: sh: vue-cli-service: command not found To resolve this issue, I have to manually crea ...

Exploring the Trio: Laravel X, Vite, and the Vue Revolution

Need assistance with Vue implementation. Struggling to interact with the template from external Js. I have these files: An app.blade.php file with a <div id="app"> container and also a @yield('scripts') outside the container for ...