vue-cli 工程中 单个slot用法

参考vue2.0文档
单个插槽使用

除非子组件模板包含至少一个 <slot> 插口,否则父组件的内容将会被丢弃。
当子组件模板只有一个没有属性的插槽时,父组件传入的整个内容片段将插入
到插槽所在的 DOM 位置,并替换掉插槽标签本身。
最初在 <slot> 标签中的任何内容都被视为备用内容。
备用内容在子组件的作用域内编译,并且只有在宿主元素为空
且没有要插入的内容时才显示备用内容。

简单理解就是:可以在子组件中使用<slot></slot>这个特殊标签,官方称开了一个插槽,
即在父组件模板中,引入的子组件标签被使用时,如果你在这个标签里加入有数据,
那么你子组件的<slot>插槽里的内容将会被替换掉

简单点,直接上代码:

在父组件中 我现在在 app.vue中


<template>
  <div id="app">
    <!-- 使用组件 -->
    <three>
      <p>发布的内容</p>
      <p>更多发布的内容</p>
    </three>
  </div>
</template>
<script>

import three from './components/three'

export default {
  components:{
    three,
  }
}
</script>

子组件three.vue

<template>
    <div class="three">
        <p>我是子组件</p>
        <slot>
            <p>如果父组件没有插件内容,我就默认显示</p>
        </slot>
    </div>
</template>

猜你喜欢

转载自blog.csdn.net/qq_36407748/article/details/80150741
今日推荐