Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to work with the mixins helper function in vue-class-compoment? #20

Open
naicha-gg opened this issue Jan 15, 2019 · 6 comments
Open

Comments

@naicha-gg
Copy link

vue-class-component: using mixins

@djkloop
Copy link

djkloop commented Jan 23, 2019

same problem

@djkloop
Copy link

djkloop commented Jan 24, 2019

@zhiquan-yu Have you solved it, please?

@naicha-gg
Copy link
Author

naicha-gg commented Jan 29, 2019

@djkloop Because we are using JSX, maybe we should use HOC like React instead of mixin.

Reference: https://github.com/MeetehS/vue-class-ts/blob/master/docs/mixins.md

@wonderful-panda
Copy link
Owner

Currently not supported.

We must specify types manually.

import Vue from "vue";
import Component, { mixins } from "vue-class-component";
import { Prop } from "vue-property-decorator";
import * as tsx from "vue-tsx-support";

interface MyMixinProps {
  foo: string;
}

@Component
class MyMixin extends Vue {
  @Prop
  foo!: string;
}

@Component
class MyComp extends mixins(MyMixin) {
  @Prop
  bar!: string;
}

const TypedMyComp = tsx.ofType<{ bar: string } & MyMixinProps>().convert(MyComp);

@djkloop
Copy link

djkloop commented Mar 26, 2019

@wonderful-panda In the future, there will be related apis in this area... I feel this @Mixins Api is very practical...

@LolliDepp
Copy link

LolliDepp commented Apr 29, 2020

I managed to use something like

MyComponent extends mixins<MyMixin, Props, Events, Slots>(MyMixin) {}

By overriding the declarations of mixins from vue-class-component. I’m considering opening a PR but I would need to add vue-class-component as a peerDependency I think... @wonderful-panda do you think this could be done? Or would you prefer if I maybe added the solution somewhere in the docs?

I managed to fix by adding this vue-class-component-extender.d.ts file:

import Vue from 'vue';
import * as tsx from 'vue-tsx-support';

declare module 'vue-class-component' {
  type VueClass<V, b = {}, c = {}, d = {}> = {
    new (...args: any[]): V & tsx.Component<b, c, d>;
  } & typeof Vue;

  function mixins<A, Props, EventsWithOn = {}, ScopedSlotArgs = {}>(
    CtorA: VueClass<A>
  ): VueClass<A, Props, EventsWithOn, ScopedSlotArgs>;
  function mixins<A, B, Props, EventsWithOn = {}, ScopedSlotArgs = {}>(
    CtorA: VueClass<A>,
    CtorB: VueClass<B>
  ): VueClass<A & B, Props, EventsWithOn, ScopedSlotArgs>;
  function mixins<A, B, C, Props, EventsWithOn = {}, ScopedSlotArgs = {}>(
    CtorA: VueClass<A>,
    CtorB: VueClass<B>,
    CtorC: VueClass<C>
  ): VueClass<A & B & C, Props, EventsWithOn, ScopedSlotArgs>;
  function mixins<A, B, C, D, Props, EventsWithOn = {}, ScopedSlotArgs = {}>(
    CtorA: VueClass<A>,
    CtorB: VueClass<B>,
    CtorC: VueClass<C>,
    CtorD: VueClass<D>
  ): VueClass<A & B & C & D, Props, EventsWithOn, ScopedSlotArgs>;
  function mixins<A, B, C, D, E, Props, EventsWithOn = {}, ScopedSlotArgs = {}>(
    CtorA: VueClass<A>,
    CtorB: VueClass<B>,
    CtorC: VueClass<C>,
    CtorD: VueClass<D>,
    CtorE: VueClass<E>
  ): VueClass<A & B & C & D & E, Props, EventsWithOn, ScopedSlotArgs>;
  function mixins<T, Props, EventsWithOn = {}, ScopedSlotArgs = {}>(
    ...Ctors: VueClass<Vue>[]
  ): VueClass<T, Props, EventsWithOn, ScopedSlotArgs>;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants