前端流行框架Vue3教程:20. 插槽slot(2)

  1. 插槽slot(2)

渲染作用域

插槽内容可以访问到父组件的数据作用域,因为插槽内容本身是在父组件模板中定义的

SlotsTow.vue

vue 复制代码
<script>
export default {
  data() {
    return {};
  }
}
</script>

<template>
  <h3>Slots续集</h3>
  <slot></slot>
</template>

App.vue

vue 复制代码
<script>

import SlotsTow from "@/components/SlotsTow.vue";
export default {
  components: {
    SlotsTow
  },
  data() {
    return {
      message: "插槽内容续集"
    }
  }
}
</script>

<template>
  <SlotsTow>
    <h3>{{ message }}</h3>
  </SlotsTow>
</template>

默认内容

在外部没有提供任何内容的情况下,可以为插槽指定默认内容

vue 复制代码
<template>
  <h3>Slots续集</h3>
  <slot>插槽默认值</slot>
</template>

具名插槽

多个插槽情况下:

SlotsTow.vue

vue 复制代码
<template>
  <h3>Slots续集</h3>
  <slot name="header">插槽默认值</slot>
  <hr>
  <slot name="main">插槽默认值</slot>
</template>

App.vue

vue 复制代码
<template>
  <SlotsTow>
    <template v-slot:header>
      <h3>{{ message }}</h3>
    </template>
    <template v-slot:main>
      <h3>内容</h3>
    </template>

  </SlotsTow>
</template>

v-slot有对应的简写#,因此<template v-slot:header>可以简写为<template #header>。其意思就是"将这部分模板片段传入子组件的header插槽中


相关推荐
mCell2 小时前
GSAP ScrollTrigger 详解
前端·javascript·动效
gnip2 小时前
Node.js 子进程:child_process
前端·javascript
excel6 小时前
为什么在 Three.js 中平面能产生“起伏效果”?
前端
excel7 小时前
Node.js 断言与测试框架示例对比
前端
天蓝色的鱼鱼8 小时前
前端开发者的组件设计之痛:为什么我的组件总是难以维护?
前端·react.js
codingandsleeping8 小时前
使用orval自动拉取swagger文档并生成ts接口
前端·javascript
石金龙9 小时前
[译] Composition in CSS
前端·css
白水清风9 小时前
微前端学习记录(qiankun、wujie、micro-app)
前端·javascript·前端工程化
Ticnix9 小时前
函数封装实现Echarts多表渲染/叠加渲染
前端·echarts
用户22152044278009 小时前
new、原型和原型链浅析
前端·javascript