前端流行框架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插槽中


相关推荐
小oo呆9 分钟前
【自然语言处理与大模型】LangChainV1.0入门指南:核心组件Messages
前端·javascript·easyui
郑州光合科技余经理11 分钟前
技术解析:如何打造适应多国市场的海外跑腿平台
java·开发语言·javascript·mysql·spring cloud·uni-app·php
果壳~38 分钟前
【前端】【canvas】图片颜色填充工具实现详解
前端
Bigger38 分钟前
Tauri (23)——为什么每台电脑位置显示效果不一致?
前端·rust·app
¥懒大王¥40 分钟前
XSS-Game靶场教程
前端·安全·web安全·xss
ssshooter44 分钟前
为什么移动端 safari 用 translate 移动元素卡卡的
前端·css·性能优化
不会飞的鲨鱼1 小时前
抖音验证码滑动轨迹原理(很难审核通过)
javascript·python
闲云一鹤1 小时前
Claude Code 接入第三方AI模型(MiMo-V2-Flash)
前端·后端·claude
惜.己1 小时前
前端笔记(四)
前端·笔记
小北方城市网1 小时前
第 5 课:Vue 3 HTTP 请求与 UI 库实战 —— 从本地数据到前后端交互应用
大数据·前端·人工智能·ai·自然语言处理