Combining component attributes with a mixin in Vue 2 using TypeScript

In my Vue + TypeScript project, we are utilizing Vue class components. Recently, I moved one of the component's methods to a separate mixin that relies on the component's properties. To address TypeScript errors regarding missing properties in the mixin, I created an interface with the same name as the mixin:

export interface ExpSelectedHandlerMixin {
  loading: boolean;
  selected: VouchersPoolTable[];
  currentApplication: string;
  items: VouchersPoolTable[];
}

@Component({})
export class ExpSelectedHandlerMixin extends Vue {
   // ...
}

I connected the mixin to my component like this:

import { Mixins } from 'vue-property-decorator';

export default class PageVouchersTable extends Mixins(ExpSelectedHandlerMixin) {
   // ...
}

However, I encountered an error message stating:

Class 'PageVouchersTable' incorrectly extends base class 'ExpSelectedHandlerMixin & Vue & object & Record<never, any>'. Type 'PageVouchersTable' is not assignable to type 'ExpSelectedHandlerMixin'. Property 'loading' is private in type 'PageVouchersTable' but not in type 'ExpSelectedHandlerMixin'.

To resolve this, I changed the access modifier of the properties (loading, selected, currentApplication, items) in the component to public by removing the private modifier.

While this solution works, I am left wondering:

Is there a way to connect a mixin that relies on component properties without having to make those properties public?

Answer №1

Opted to eliminate unnecessary interface declaration in the mixin file and instead keep shared properties as public in the component. The finalized code looks like this:

// Mixin
@Component({})
export default class ExpSelectedHandlerMixin extends Vue {
  loading = false

  items: VouchersPoolTable[] = []

  selected: VouchersPoolTable[] = []

  // ...
}

// Component
import { Mixins } from 'vue-property-decorator';

@Component({})
export default class PageVouchersTable extends Mixins(ExpSelectedHandlerMixin) {

  // These three properties below are made public for accessibility in ExpSelectedHandlerMixin
  loading = false

  items: VouchersPoolTable[] = []

  selected: VouchersPoolTable[] = []

  // ...
}

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

What is the best way to broaden the capabilities of function objects through the use of

Below is a code snippet that raises the question of how one should define certain types. In this scenario, it is required that Bar extends Foo and the return type of FooBar should be 'a'. interface Foo { (...args: any):any b: string } i ...

The validation through class-validator or class-transformer fails to function properly when applying multiple Types to the @Query decorator

Is there a way to combine multiple types with the @Query() decorator in my controller, such as ParamsWithRegex and PaginationParams? I'm facing an issue where no validation is being applied when I do that. How can I resolve this problem? **// MY CON ...

What is the best way to send props (or a comparable value) to the {children} component within RootLayout when using the app router in Next.js

As I work on updating an e-commerce website's shopping cart to Next.js 13+, I refer back to an old tutorial for guidance. In the previous version of the tutorial, the _app.ts file utilized page routing with the following code snippet: return ( < ...

Why is the function not being called when declaring a variable, resulting in a type error?

click here reference: const fn1 = (arg1: { key: number, })=>{ console.log(arg1) } fn1({ key: 1 }) const data = { key: 1, a: 1, } fn1(data) fn1({ key: 1, a: 1, }) more info Assistance needed: Why is ...

Verifying updates in the watchlist once data has been initialized upon mounting

When trying to edit a component, the data is loaded with mounted in the following way: mounted () { var sectionKey = this.$store.getters.getCurrentEditionKey this.table = _.clone(this.$store.getters.getDocumentAttributes[sectionKey]) this.ta ...

The 'locale' parameter is inherently assigned the type of 'any' in this context

I have been using i18n to translate a Vue3 project with TypeScript, and I am stuck on getting the change locale button to work. Whenever I try, it returns an error regarding the question title. Does anyone have any insights on how to resolve this issue? ...

Issue with Installing Vue CLI Plugin Vue-ChartJS

For my project, I am utilizing Vue CLI and planning to incorporate the vue-chartjs plugin. In order to set it up, I followed this instructional guide: . However, upon running the process, I encountered an error with the npm package: ...

When the class changes, V-for re-renders every component

Currently, I am in the process of changing classes for images based on their index using the moveIndex method and key events. While this works smoothly on computers, it seems to take significantly longer when implemented on TVs. The process runs smoothly w ...

Having trouble getting nuxt-link to function properly while attempting to display the _slug.vue page in Nuxt using a combination of Apollo and WPGraphQL

I am struggling with setting up my Nuxt/Vue app that uses Apollo to query WPGraphQL on Wordpress. Specifically, I am having difficulty configuring my Nuxt-link on the index page to route to my _slug.vue page. When I manually enter the URL in the browser us ...

Can Vue.js support two-way data-binding without the use of an input element?

Here is the code snippet that I'm working with: <div id="app"> {{ message }} </div> JavaScript: myObject = {message:"hello"} new Vue({ el: '#app', data: myObject }) When I update myObject.message, the content within th ...

Exploring VueJS by enhancing lifecycle hooks in each component

My app features a loader screen that displays before the content is rendered, and then disappears once everything has loaded. While this setup works well for a few components or views, I am finding it tedious to add this functionality to every single one. ...

Issue with Vue router: Unable to retrieve API data after redirection

I am currently developing an application that utilizes JWT Login. I have implemented a check to see if the user is logged in when they visit the homepage ("/"), and then redirect them to the Dashboard: let routes = [ { path: '', component: L ...

Guide to launching Vue CLI using Webpack on a live server

Just yesterday, I purchased a droplet from Digital Ocean in order to experiment with new concepts directly on a live server. My first venture was into Vue development. I meticulously followed the Vue CLI and Webpack installation guides, all without any hic ...

Utilizing a solo attribute within a Vue3 composable

I'm currently using a composable function to load images in my Vue3 project. While I've been able to successfully pass all the props as one object, you can check this question for reference, I'm facing an issue with passing a specific proper ...

What is the best way to differentiate between the content in the 'stories' and '.storybook' directories?

Overview Upon integrating Storybook.js with my existing Create-React-App project, I found that two new directories were created by default: .storybook src/stories This integration seemed to blur the lines between different aspects of my project, which g ...

I forgot to delete the page after changing the route in Nuxt

I am currently working on a simple page that contains a function calling an API. Despite changing the page, this function persists and continues to call the API method again myFunction async getFlights() { try { const { data } = await this.$axios.ge ...

Display a 404 page on Vue router when the ID does not match

When it comes to displaying a 404 page if a parameter doesn't match, I'm a bit puzzled. For instance, if the user name is “joe” and someone tries to access “/joe/categories”, then the categories component should be displayed. Now, I want ...

What is the best way to retrieve the href attribute when working with cheerio?

Is there a way to retrieve the link using Cheerio in this code snippet? <div class="someClass"> <a href="someLink">Link</a> </div> I attempted to do so, but unfortunately it was unsuccessful. let link = $(&a ...

Issue with vue-apollo package causing GraphQL query not to display on the frontend

Currently, I am expanding my knowledge of GraphQL and working on a project where I aim to display queries in the front end. To achieve this, I have incorporated the package GitHub - Akryum/vue-apollo: ...

What could be causing the Typescript error when utilizing useContext in combination with React?

I am currently working on creating a Context using useContext with TypeScript. I have encapsulated a function in a separate file named MovieDetailProvider.tsx and included it as a wrapper in my App.tsx file. import { Context, MovieObject } from '../in ...