有没有办法在VueJS中使用mixins继承模板

Ali*_*Ali 8 javascript vue.js

有人知道如何mixin使用其模板继承a ?或如何从一个mixin?注入dinamically元素或组件?

编辑: 我不想修改问候组件,我有两个Mixins:404Mixin添加方法raise404()并显示100%图层和LoaderMixin,其中有load()方法显示角落中的微调器.我可以继承他们的方法,但我必须在我想要使用它的每个组件中复制html.

谢谢

mixin = {
  template: '<p>{{ foo }}</p>',
  data() {
    return {
      foo: 'Hello',
    };
  },
}

// This should  be <div><p>Hello</p><p>World!</p></div>
Vue.component('greeting', {
  mixins: [mixin],
  template: '<div><p>World!</p></div>'
});

var vm = new Vue({
  el: '#app'
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdn.jsdelivr.net/vue/1.0.16/vue.js"></script>
<div id="app">
  <greeting></greeting>
</div>
Run Code Online (Sandbox Code Playgroud)

Ali*_*Ali 9

多年后,我可以想象出一个优雅的解决方案,也许使用类、打字稿或在 mixin 中创建组件 super 的注释可能会更优雅,但现在,问题已部分解决。

GreetingMixin = {
  data() {
    return {
      greeting: 'Hello',
    };
  },
  provide() { return {child: this}},
  components: {
    super: {
      inject: ['child'],
      template: '<div class="blue">{{ child.greeting }} <strong><slot /></strong></div>',
    }
  },
}


// This should be <div class="blue">Hello <strong>World!</strong></div>
Vue.component('welcomeWorld', {
  mixins: [GreetingMixin],
  template: '<super>World!</super>',
});

// This should be <div class="blue">Hi <strong><i>ali</i></strong></div>
Vue.component('welcomeName', {
  mixins: [GreetingMixin],
  props: ["name"],
  created() { this.greeting = "Hi" },
  template: '<super><i>{{ name }}</i></super>',
});

// This should be <h1><div class="blue">Hello <strong>World</strong></div></h1>
Vue.component('welcomeH1', {
  mixins: [GreetingMixin],
  props: ["name"],
  template: '<h1><super>{{ name }}</super></h1>',
});


var vm = new Vue({
  el: '#app'
});
Run Code Online (Sandbox Code Playgroud)
.blue {
color: blue
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <welcome-world></welcome-world>
  <welcome-name name="Ali"></welcome-name>
  <welcome-h1 name="Ali"></welcome-h1>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 这看起来很有用,但我不明白它是如何以及为什么起作用的。您能给一些参考或解释吗?....它实际上创建了一个供“greeting”使用的本地组件吗?“super”模板渲染后,“greeting”的成员是否可用?...好吧,做了一个实验...他们会的,我将用扩展示例编辑您的答案。 (2认同)

Dec*_*oon 5

您不能像您的示例那样“继承”混合模板,如果可能的话,必须有一种标准化的方式来合并模板。

既然您似乎真正想要做的就是继承模板,那么为什么不使用带有插槽的组件组合呢?

Vue.component('not-found', {
  template: '#not-found',
  methods: {
    doSomethingSpecial() {
      alert('Hi there');
    },
  },
});

new Vue({
  el: '#app',
  data() {
    return {
      notFoundVisible: false,
    };
  },
});
Run Code Online (Sandbox Code Playgroud)
.not-found {
  background-color: white;
  text-align: center;
  font-size: 30px;
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdn.jsdelivr.net/vue/1.0.16/vue.js"></script>

<template id="not-found">
  <div class="not-found">
    <h1>404 Not Found</h1>
    <slot></slot>
  </div>
</template>

<div id="app">
  <not-found v-show="notFoundVisible" @click="notFoundVisible = false" v-ref:not-found>The resource was not found</not-found>
  <button @click="notFoundVisible = true">Click Me</button>
  <button @click="$refs.notFound.doSomethingSpecial()">Do Something Special</button>
</div>
Run Code Online (Sandbox Code Playgroud)

有什么特殊的原因为什么您需要混合这些组件而不是将它们组合在一起?

  • 是的,我有类似的东西,但是我在 $refs 之前使用了加载方法,并且为了它起作用我必须使用道具和自定义方法然后它变得太混乱然后我决定使用 mixins 我想知道我是否可以更多透明的。 (2认同)