美文网首页
vue 组件插槽

vue 组件插槽

作者: 杨晨1994 | 来源:发表于2020-04-14 17:35 被阅读0次

一、vue插槽有什么用

插槽语法是vue中实现内容分发的api,用于复合组件开发。该技术在通用组件库开发中有大量应用

二、匿名插槽

// 子组件
<template>
  <div>
    <slot></slot>
  </div>
</template>
// 父组件
<hello-world>
    <p>hello world</p>
</hello-world>

三、具名插槽

// 子组件 一个不带 name 的 slot 出口会带有隐含的名字default。
<template>
  <div>
    <slot name="header"></slot>
    <slot></slot>
    <slot name="footer"></slot>
  </div>
</template>
// 父组件
<template>
   <div>
      <hello-world>
        <template v-slot:header>
          我是header
        </template>
        <template>
          我是default
        </template>
        <template v-slot:footer>
          我是footer
        </template>
      </hello-world>
   </div>
</template>

四、作用域插槽

有时让插槽内容能够访问子组件中才有的数据是很有用的

// 子组件
<template>
  <div>
    <slot name="header" :msg="msg" :val="val"></slot>
    <slot></slot>
    <slot name="footer"></slot>
  </div>
</template>
// 父组件 slotProps可以替换
<template>
   <div>
      <hello-world>
        <template v-slot:header="slotProps">
          {{slotProps.msg}}{{slotProps.val}}
        </template>
        <template>
          我是default
        </template>
        <template v-slot:footer>
          我是footer
        </template>
      </hello-world>
   </div>
</template>

相关文章

  • vue插槽slot

    -具名插槽 子组件Demo.vue 引用子组件的页面 -作用域插槽 子组件Demo.vue 引用子组件的页面

  • 【Vue】组件 - 插槽默认值

    基础知识 【Vue】组件 - 插槽的基本用法 【Vue】组件 - 多个插槽 子组件里,在 里写上默认的内容。 在父...

  • 2020-07-23 一次性讲明白vue插槽slot

    vue插槽slot 一、前言 vue官方文档中在"组件基础"内容中提到组件可以通过插槽分发内容,那插槽是怎么使用的...

  • vue插槽slot

    vue插槽slot 一、前言 vue官方文档中在"组件基础"内容中提到组件可以通过插槽分发内容,那插槽是怎么使用的...

  • Vue总结4-插槽,Vuex,VueRouter

    1.插槽 1.1匿名插槽 52-Vue组件-匿名插槽 ...

  • Vue插槽

    Vue 插槽 插槽用于向组件传递内容,插槽内可以包含任何模板代码,包括 HTML,甚至组件。如果 组件 没有包含一...

  • 插槽v-slot

    父子组件插槽的使用 父组件product.vue 子组件 ProductParam.vue

  • vue具名插槽使用

    插槽用法:子组件header固定;父组件outside插槽header/index.vue outside/ind...

  • vue 插槽

    插槽语法是Vue实现的内容分发API,用于复合组件开发。 匿名插槽 具名插槽 作用域插槽 将内容分发到子组件指定位置

  • Vue3:Slot 插槽

    1、匿名插槽 1.1 什么是插槽 插槽(Slot)是 vue 为组件的封装者提供的能力。允许开发者在封装组件时,把...

网友评论

      本文标题:vue 组件插槽

      本文链接:https://www.haomeiwen.com/subject/bbhtvhtx.html