Skip to content

Vue Mixin and multiple Vue hooks, supported? #116

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

Closed
304NotModified opened this issue Jul 3, 2017 · 3 comments
Closed

Vue Mixin and multiple Vue hooks, supported? #116

304NotModified opened this issue Jul 3, 2017 · 3 comments

Comments

@304NotModified
Copy link
Contributor

304NotModified commented Jul 3, 2017

I have the Mixins working (from #91) - thanks! great feature!

in my solution I need multiple Vue hooks, e.g. multiple created. This is working (all 3 created are called), but is it supported/by-design? If it's breaks, then I have a big issue ;)

Thanks in advance!

Code example:

//---------------- BEGIN  mixin helpers --------------
/**
 * Created by jsons on 2017/5/27.
 */
import { Vue } from 'vue/types/vue'

import { ComponentOptions, FunctionalComponentOptions } from 'vue/types/options'

import { componentFactory } from "vue-class-component/lib/component";
import { Component } from "vue-property-decorator";

function ComponentForMixin<V, U extends Vue>(options: ComponentOptions<U> | V): any {
    if (typeof options === 'function') {
        return componentFactory(options as any)
    }
    return function (Component: V) {
        return componentFactory(Component as any, options)
    }
}

type VClass<T extends Vue> = {
    new (): T
    extend(option: ComponentOptions<Vue> | FunctionalComponentOptions): typeof Vue
}

function Mixin<T extends Vue>(parent: typeof Vue, ...traits: (typeof Vue)[]): VClass<T> {
    return parent.extend({ mixins: traits }) as any
}

//---------------- END mixin helpers --------------

@Component
class Pen extends Vue {
    created(): void { // <-------- created 1
        console.log("created Pen")
    }
}
@Component
class Apple extends Vue {
   created(): void { //<-------- created 2
        console.log("created Apple")
    }
}

interface IApplePenTrait extends Pen, Apple {

    //in my solution I also need here: 
    // created(): void
    //otherwise I get the error:
    //TS2320: Interface 'IApplePenTrait' cannot simultaneously extend types 'Pen' and 'Apple'.
    //Named properties 'created' of types 'Pen' and 'Apple' are not identical.
    //Dunno why not needed here
}


// compiles under TS2.2
@ComponentForMixin({
    template: `<span @click="Uh"> click show</span>`
})
export default class ApplePen extends Mixin<IApplePenTrait>(Apple, Pen) {
    created(): void { //<-------- created 3
        console.log("created ApplePen")
    }
}

PS I would like to set-up a code pen, but I can't get the imports working.

@ktsn
Copy link
Member

ktsn commented Jul 3, 2017

Sorry, I don't get your point.
You say that your code is working but what is your issue?

@304NotModified
Copy link
Contributor Author

304NotModified commented Jul 3, 2017

You say that your code is working but what is your issue?
Well I'm not sure if it works "by accident" or "by-design".

In the code example the same method, created, with another body, is included. If those were really Typescript mixins, that would be a compile error? (or just overwrite it with the "latest" definition)

Let me know if it's still not clear, then I will try to explain it differently.

@ktsn
Copy link
Member

ktsn commented Jul 3, 2017

Thanks for your answer.
It's by design. Because vue-class-component is just like a syntax sugar of Vue.extend.
It should work as same as Vue.extend.

@ktsn ktsn closed this as completed Jul 3, 2017
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

2 participants