008-slot插槽

插槽就是子组件中的提供给父组件使用的一个占位符,用<slot></slot> 表示,父组件可以在这个占位符中填充任何模板代码,如 HTML、组件等,填充的内容会替换子组件的标签。
简单理解就是子组件中留下个“坑”,父组件可以使用指定内容来补“坑”。

1、插槽 slot 的简单使用

app.vue 文件引入 Son.vue 组件:

<template>
  <div id="app">
    app component
    <Son>
      <div class="slot-contant">插槽内容</div>
    </Son>
  </div>
</template>

<script>
import Son from './components/SlotComponents/Son'
export default {
  name: 'App',
  components: { Son }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  background: #4a90e2;
  color: #fff;
  padding: 20px;
}
.slot-contant {
  color: #f00;
  font-size: 16px;
}
</style>

Son.vue文件内容:

<template>
  <div class="son-component">
    son header
    <slot />
    son footer
  </div>
</template>
<script>
export default {}
</script>
<style scoped>
.son-component {
  width: 200px;
  height: 200px;
  padding: 20px;
  background: #ccc;
  border: 1px solid #ccc;
}
</style>

页面展示效果:
在这里插入图片描述

2、插槽分类

2.1 默认插槽

<slot />

2.2 具名插槽

💡 Tips:有名字的插槽,子组件可以在多个地方插入不同内容

<slot name="header"></slot>
<slot name="footer"></slot>

<!-- 父组件使用 -->
<template v-slot:header></template>
<template #footer></template>
<template #default></template>

举例说明:父组件

<template>
  <div id="app">
    app component
    <Son>
      <template #header>
        <p>我是header部分</p>
      </template>

      <p>我是main(默认插槽)部分</p>

      <template v-slot:footer>
        <p>我是footer部分</p>
      </template>
    </Son>
  </div>
</template>

<script>
import Son from './components/SlotComponents/Son'
export default {
  name: 'App',
  components: { Son }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  background: #4a90e2;
  color: #fff;
  padding: 20px;
}
.slot-contant {
  color: #f00;
  font-size: 16px;
}
</style>

Son.vue 子组件:

<template>
  <div class="son-component">
    <header>
      <slot name="header"></slot>
    </header>
    <main>
      <slot></slot>
    </main>
    <footer>
      <slot name="footer"></slot>
    </footer>
  </div>
</template>
<script>
export default {}
</script>
<style scoped>
.son-component {
  width: 200px;
  height: 200px;
  padding: 20px;
  background: #ccc;
  border: 1px solid #ccc;
}
</style>

页面效果:
在这里插入图片描述

2.3 作用域插槽

💡 Tips:插槽内容能够访问子组件中才有的数据

<slot :msg="msg"></slot>

<!-- 父组件使用 -->
<template v-slot:default="data">
  <p>{{ data.msg }}</p>
</template>
<template v-slot="data">
  <p>{{ data.msg }}</p>
</template>

举例说明:父组件

<template>
  <div id="app">
    app component
    <Son>
      <template v-slot="data">
        <p>{{ data.msg }}</p>
      </template>
    </Son>
  </div>
</template>

<script>
import Son from './components/SlotComponents/Son'
export default {
  name: 'App',
  components: { Son }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  background: #4a90e2;
  color: #fff;
  padding: 20px;
}
.slot-contant {
  color: #f00;
  font-size: 16px;
}
</style>

Son.vue 子组件:

<template>
  <div class="son-component">
    <slot :msg="msg"></slot>
  </div>
</template>
<script>
export default {
  data() {
    return {
      msg: 'hello, i am son'
    }
  }
}
</script>
<style scoped>
.son-component {
  width: 200px;
  height: 200px;
  padding: 20px;
  background: #ccc;
  border: 1px solid #ccc;
}
</style>

页面效果:
在这里插入图片描述

  • 9
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值