简体   繁体   English

如何在带有单个文件组件的 HTML 中使用插槽

[英]How to use slots in the HTML with Single File Components

I want to use slots in Vue to create a dynamic modal component.我想在 Vue 中使用插槽来创建动态模态组件。

I already tried a lot of tutorials of Vue / slots, but none of them is exactly what i'm looking for.我已经尝试了很多 Vue / slot 教程,但没有一个是我正在寻找的。

This is a piece of my modal.vue :这是我的modal.vue的一部分:

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

This is my javascript compiled file:这是我的 javascript 编译文件:

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

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

This is piece of my HTML file:这是我的 HTML 文件的一部分:

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

I was expecting that all elements present inside #modal ( #dynamic-input in this case), were inserted into the slot named modal-body , inside my Vue element.我期待#modal中存在的所有元素(在本例中为#dynamic-input )都插入到我的 Vue 元素内名为modal-body的插槽中。 Is it possible to do it?有可能做到吗? Am i missing something?我错过了什么吗?

Check what version of Vue you are using.检查您使用的 Vue 版本。 Thenamed slot syntax changed in 2.6.0. 命名槽语法在 2.6.0 中发生了变化。 Consider the differences below.考虑以下差异。 One uses render functions and the other template Strings.一种使用渲染函数,另一种使用模板字符串。

Vue@2.6.10 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 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM