【问题标题】:How to use slots in the HTML with Single File Components如何将 HTML 中的插槽与单个文件组件一起使用
【发布时间】:2019-10-27 07:56:02
【问题描述】:

我想在 Vue 中使用 slot 来创建动态模态组件。

我已经尝试了很多关于 Vue/slots 的教程,但没有一个是我想要的。

这是我的modal.vue

<template>
  ...
    <slot name="modal-body"></slot>
  ...
</template>
<script>
</script>
<style>
</style>

这是我的 javascript 编译文件:

import Vue from 'vue';
import modal from './modal.vue';

new Vue({
  el: '#modal',
  render: r => r(modal)
});

这是我的 HTML 文件的一部分:

...
<div id="modal">
  <template v-slot="modal-body">
    <input type="text" id="dynamic-input">
  </template>
</div>
...

我期待 #modal(在本例中为 #dynamic-input)中存在的所有元素都插入到我的 Vue 元素内名为 modal-body 的插槽中。 有可能做到吗?我错过了什么吗?

【问题讨论】:

  • 其实你用的是什么版本的Vue?
  • 我使用的是 2.6.10
  • 我尝试了其他一些版本,但它们也不起作用。

标签: javascript vuejs2 vue-cli


【解决方案1】:

检查您使用的 Vue 版本。 named slot syntax 在 2.6.0 中更改。考虑以下差异。一个使用渲染函数,另一个使用模板字符串。

Vue@2.6.10

// Example using template String
const modalTemplateString = {
  template: "<div><div>above</div><slot name=\"modal-body\"></slot><div>below</div></div>"
};

const appTemplateString = new Vue({
  el: "#appTemplateString",
  components: {
    modal: modalTemplateString
  },
  template: "<div><modal><template v-slot:modal-body><div>foobar</div></template></modal></div>"
});

// Example using render function
const modalRenderFunc = {
  render(h) {
    return h("div", [
      h("div", "above"),
      h("div", this.$slots["modal-body"]),
      h("div", "below")
    ]);
  }
}

const appRenderFunc = new Vue({
  el: "#appRenderFunc",
  components: {
    modal: modalRenderFunc
  },
  render(h) {
    return h("div", [
      h("modal", [
        h("div", {
          slot: "modal-body"
        }, "foobar")
      ])
    ]);
  }
});
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script>

<h2>Template String</h2>
<div id="appTemplateString"></div>
<hr/>
<h2>Render Function</h2>
<div id="appRenderFunc"></div>

Vue@2.5.22

// Example using template String
const modalTemplateString = {
  template: "<div><div>above</div><slot name=\"modal-body\"></slot><div>below</div></div>"
};

const appTemplateString = new Vue({
  el: "#appTemplateString",
  components: {
    modal: modalTemplateString
  },
  template: "<div><modal><template slot=\"modal-body\"><div>foobar</div></template></modal></div>"
});

// Example using render function
const modalRenderFunc = {
  render(h) {
    return h("div", [
      h("div", "above"),
      h("div", this.$slots["modal-body"]),
      h("div", "below")
    ]);
  }
}

const appRenderFunc = new Vue({
  el: "#appRenderFunc",
  components: {
    modal: modalRenderFunc
  },
  render(h) {
    return h("div", [
      h("modal", [
        h("div", {
          slot: "modal-body"
        }, "foobar")
      ])
    ]);
  }
});
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.22/dist/vue.js"></script>

<h2>Template String</h2>
<div id="appTemplateString"></div>
<hr/>
<h2>Render Function</h2>
<div id="appRenderFunc"></div>

【讨论】:

  • 不错!但这只是我想做的一半。考虑而不是只写一个字符串(在你的例子中是“foobar”),我有一个完整的表单可以插入到模态体中。此外,表单不会在 javascript 文件中,而是在 HTML 中。我想知道是否有可能以某种方式抓取“
  • 我在我的环境中测试了你的例子,HTML没有被解析,它只是文本=/
  • 有人吗?我使用了另一种方法,但这种解决方案有时会很有用。
  • 人?有人来帮忙吗? =/
  • 如果可以,请随时提供帮助 =)
猜你喜欢
相关资源
最近更新 更多
热门标签