首页 > 其他分享 >Vue 组件插槽

Vue 组件插槽

时间:2022-09-19 08:45:01浏览次数:63  
标签:body Vue name footer 插槽 header 组件

默认插槽的定义与使用

<!-- 组件 test 定义 -->
<template>
    <button>
        <slot>提交</slot>
    </button>
</template>


<!-- 父组件调用 -->
<template>
    <test>保存</test>
</template>

 

具名插槽的定义与使用

<!-- 组件 test 定义 -->
<template>
    <div class="header-container">
        <slot name="header"></slot>
    </div>
    
    <div class="body-container">
        <slot name="body"></slot>
    </div>
    
    <div class="footer-container">
        <slot name="footer"></slot>
    </div>
</template>


<!-- 父组件调用 -->
<template>
    <test>
        <template #header>
            <div class="bg-primary text-white">这是头部</div>
        </template>
        
        <template #body>
            <div class="bg-light text-danger">这是主体部份</div>
        </template>
        
        <template #footer>
            <div class="bg-info text-primary">这是底部</div>
        </template>
    </test>
</template>

 

 

动态插槽名的定义

<template v-slot:[dynamicSlotName]>...</template>

 

 

插槽内容访问数据说明:

一、默认下插槽内容无法访问子组件的数据。其只能访问当前父组件数据。

二、但是我们可以通过以下方式,使插槽内容能够访问子组件的数据:

<!-- 组件 test 定义 -->
<template>
    <div class="header-container">
        <!-- 在slot 上,绑定数据属性 -->
<!-- 注意: name 属性是系统属性,它不会传递给插槽内容 --> <slot name="header" :title="data.title" :date="data.date"></slot> </div> <div class="body-container"> <slot name="body" :text="data.text"></slot> </div> <div class="footer-container"> <slot name="footer" :copyright="data.copyright"></slot> </div> </template> <script setup> import { ref } from 'vue' const data = ref({ title: '文章标题', date: '发表时间', text: '文章正文', copyright: '版权声明' }) </script> <!-- 父组件调用 test --> <template> <test> <!-- 在这里指定 slot的name 对应的插槽必需对应的name属性值,比如这里将header改成body是错误的 --> <template #header="header"> <!-- 这里是 slot.属性 的取值方式 --> <div class="bg-primary text-white">{{header.title}}</div> </template> <template #body="body"> <div class="bg-light text-danger">{{body.text}}</div> </template> <template #footer="footer"> <div class="bg-info text-primary">{{footer.copyright}}===这是底部</div> </template> </test> </template>

 

标签:body,Vue,name,footer,插槽,header,组件
From: https://www.cnblogs.com/wm218/p/16706495.html

相关文章

  • vue中循环table表格数据,可编辑的列表(新增、删除、修改)
    原文链接:https://blog.csdn.net/qq_24441205/article/details/1261426691.如何循环如下图table表格数据(注:emerResponseDetail为整个列表定义的数据对象)因"作业人员"为......
  • 29. Activity组件
    29.Activity组件29.1认识Activity应用画面→ActivityActivity是一个应用程序组件,提供一个屏幕,用户可以用来交互为了完成某项任务。Activity中所有操作都与用户密切相......
  • 30. Service组件
    30.Service组件30.1认识Service服务在后台默默地运行,是不可见的。30.2startService与生命周期新建一个Activity在清单文件中设置Activity3默认启动MyService.j......
  • 31. Receiver组件
    31.Receiver组件31.1认识Receiver广播:分为系统广播与用户自定义广播系统广播:开机广播、网络状态广播、蓝牙…31.2静态注册接收广播定义一个广播接收者package......
  • vue知识点
    一、slot实现原理https://segmentfault.com/a/1190000019652989 二、keep-alive实现原理https://segmentfault.com/a/1190000021942204?sort=voteshttps://blog.csdn.ne......
  • 【C#】组件发布:MessageTip,轻快型消息提示窗
    【C#】组件发布:MessageTip,轻快型消息提示窗 -------------201610212046更新-------------更新至2.0版,基本完全重写,重点:改为基于原生LayeredWindow窗体和UpdateLayered......
  • vue3 基础-具名插槽 & 作用域插槽
    上篇对slot的基本概念和使用有一个初步的认识,即通过slot的这种设计,父组件可以在调用子组件的时候,给组件之间传递一波dom,子组件通过slot标签来进行接收.sl......
  • 前端_vue
    路由怎么传参,有哪几种方式?第一种:需要在路由配置this.$router.push({//传参path:`/chat/${session.id}`,})路由配置{path:'/chat/:sessionId',nam......
  • 【VSCode】vue中配置@的自动提示
    1、效果2、安装插件插件名称:PathAutocomplete3、打开setting.json文件4、setting.js文件里新增配置//导入文件时是否携带文件的扩展名"path-autocomp......
  • SpringBoot校园博客系统 博客管理系统 开源博客系统 个人博客系统Java Vue MySQL数据
    ......