Is there way to inherit templates with mixins in VueJS

后端 未结 2 1184
北恋
北恋 2020-12-31 08:04

Someone knows how inherit a mixin with its template? or how to inject dinamically elements or components from a mixin?

EDIT:

2条回答
  •  旧时难觅i
    2020-12-31 08:49

    After years, I can imagine a elegant solution, and maybe it could be more elegant using classes, typescript or an annotation that create the component super in the mixin, but for now, the problem is partial solved.

    GreetingMixin = {
      data() {
        return {
          greeting: 'Hello',
        };
      },
      provide() { return {child: this}},
      components: {
        super: {
          inject: ['child'],
          template: '
    {{ child.greeting }}
    ', } }, } // This should be
    Hello World!
    Vue.component('welcomeWorld', { mixins: [GreetingMixin], template: 'World!', }); // This should be
    Hi ali
    Vue.component('welcomeName', { mixins: [GreetingMixin], props: ["name"], created() { this.greeting = "Hi" }, template: '{{ name }}', }); // This should be

    Hello World

    Vue.component('welcomeH1', { mixins: [GreetingMixin], props: ["name"], template: '

    {{ name }}

    ', }); var vm = new Vue({ el: '#app' });
    .blue {
    color: blue
    }
    
    

提交回复
热议问题