vue3的插槽全家桶

简介: vue3的插槽全家桶

vue3的插槽全家桶
10/100

「笔耕不辍」-生命不止,写作不息

发布文章
weixin_45441173
未选择任何文件
new

插槽:父组件引入(import)子组件 模板绑定子组件 在子组件标签内传值 子组件文件内定义插槽(slot)进行接收

在这里插入图片描述

子组件

<template>
    <div style="">
          //这是H5的语义化标签哦不是封装的什么组件 不要误解
           <header style="width:100%;height:50px;background:red"> 
             <slot name="header"/>
           </header>
            <main style="width:100%;height:50px;background:green">
               <slot name="main"/>
            </main>
          <footer style="width:100%;height:50px;background:yellow">
            <slot name="footer" />     
          </footer>
           <div class="" style="width:100%;height:150px;background:blue">
                 <div class="" v-for="(item,index) in  dataslot " :key="item.name">
                     <slot :dates="item" :index="index" />
                 </div>
           </div> 
    </div>
</template>

<script setup lang="ts">
import  {reactive} from 'vue'
type slots={
    name:string,
    age:number
}
const  dataslot=reactive<slots[]>([
    {name:'小小被插入了1',age:100},
    {name:'小小被插入了2',age:200},
    {name:'小小被插入了3',age:300},
])
</script>

<style scoped>

</style>

父组件

 <template> 
     <Dialog>
    <!--
     v-slot简写:
     不可以在template上使用slot='xxx'必须v-slot:'xxx'
     写法1:
       #(符号)   eg(举例):v-slot:main=等同于=> #main 
      v-slot="{data,index}"==>#default="{data,index}"   {解构} 
      
      写法2:
      ts定义  let  nameslot=ref('main')
      模板绑定:#[nameslot]  ||  v-slot:[nameslot]    [nameslot]==>'main'
    -->
    <template #header>
        <div>
            我被插槽了上面
        </div>
    </template>
     <!--看script内定义这个变量    let  nameslot=ref('main')-->  
     <!-- <template v-slot:[nameslot]> -->
     <!-- 或者 --> 
     <template #[nameslot]>
        <div>
            我被插槽了中间 
        </div>
    </template>
    
     <template v-slot:footer>
        <div>
            我被插槽了下面
        </div>
    </template>

     <!-- <template v-slot="{dates,index}"> -->
         <!-- 或者 -->
     <template #default="{dates,index}">
        <div>
             {{dates}}--{{index}}
        </div>
    </template>
</Dialog>
</template>

<script setup lang="ts">
import  {reactive ,markRaw,ref} from 'vue'
import  Dialog  from '../Dialog/index.vue' 
// 插槽简写
let  nameslot=ref('main') 
</script>

<style scoped lang="less">
.bg-red{
    background: red;
}
.content{
    flex: 1;
     display: flex;
    // height: 100vh;
    margin: 20px;
    border-bottom: 1px solid #ccc;
    overflow: auto;
    &-items{
        padding:20px;border-bottom: 1px solid #ffffff;
    }
     .tab{
         height: 50px;
        border:1px solid red;
     }
     component{
         height: 30px;;
     }
}
</style>

插槽:父组件引入(import)子组件 模板绑定子组件 在子组件标签内传值 子组件文件内定义插槽(slot)进行接收
在这里插入图片描述

子组件

<div style="">
      //这是H5的语义化标签哦不是封装的什么组件 不要误解
       <header style="width:100%;height:50px;background:red"> 
         <slot name="header"/>
       </header>
        <main style="width:100%;height:50px;background:green">
           <slot name="main"/>
        </main>
      <footer style="width:100%;height:50px;background:yellow">
        <slot name="footer" />     
      </footer>
       <div class="" style="width:100%;height:150px;background:blue">
             <div class="" v-for="(item,index) in  dataslot " :key="item.name">
                 <slot :dates="item" :index="index" />
             </div>
       </div> 
</div>

name:string,
age:number

}
const dataslot=reactive<slots[]>([

{name:'小小被插入了1',age:100},
{name:'小小被插入了2',age:200},
{name:'小小被插入了3',age:300},

])


父组件

 <Dialog>
<!--
 v-slot简写:
 不可以在template上使用slot='xxx'必须v-slot:'xxx'
 写法1:
   #(符号)   eg(举例):v-slot:main=等同于=> #main 
  v-slot="{data,index}"==>#default="{data,index}"   {解构} 
  
  写法2:
  ts定义  let  nameslot=ref('main')
  模板绑定:#[nameslot]  ||  v-slot:[nameslot]    [nameslot]==>'main'
-->
<template #header>
    <div>
        我被插槽了上面
    </div>
</template>
 <!--看script内定义这个变量    let  nameslot=ref('main')-->  
 <!-- <template v-slot:[nameslot]> -->
 <!-- 或者 --> 
 <template #[nameslot]>
    <div>
        我被插槽了中间 
    </div>
</template>

 <template v-slot:footer>
    <div>
        我被插槽了下面
    </div>
</template>

 <!-- <template v-slot="{dates,index}"> -->
     <!-- 或者 -->
 <template #default="{dates,index}">
    <div>
         {{dates}}--{{index}}
    </div>
</template>


background: red;

}
.content{

flex: 1;
 display: flex;
// height: 100vh;
margin: 20px;
border-bottom: 1px solid #ccc;
overflow: auto;
&-items{
    padding:20px;border-bottom: 1px solid #ffffff;
}
 .tab{
     height: 50px;
    border:1px solid red;
 }
 component{
     height: 30px;;
 }

}

Markdown 1889 字数 123 行数 当前行 1, 当前列 0HTML 1785 字数 104 段落

相关文章
|
2月前
|
JavaScript 前端开发 安全
Vue 3
Vue 3以组合式API、Proxy响应式系统和全面TypeScript支持,重构前端开发范式。性能优化与生态协同并进,兼顾易用性与工程化,引领Web开发迈向高效、可维护的新纪元。(238字)
573 139
|
2月前
|
缓存 JavaScript 算法
Vue 3性能优化
Vue 3 通过 Proxy 和编译优化提升性能,但仍需遵循最佳实践。合理使用 v-if、key、computed,避免深度监听,利用懒加载与虚拟列表,结合打包优化,方可充分发挥其性能优势。(239字)
268 1
|
7月前
|
缓存 JavaScript PHP
斩获开发者口碑!SnowAdmin:基于 Vue3 的高颜值后台管理系统,3 步极速上手!
SnowAdmin 是一款基于 Vue3/TypeScript/Arco Design 的开源后台管理框架,以“清新优雅、开箱即用”为核心设计理念。提供角色权限精细化管理、多主题与暗黑模式切换、动态路由与页面缓存等功能,支持代码规范自动化校验及丰富组件库。通过模块化设计与前沿技术栈(Vite5/Pinia),显著提升开发效率,适合团队协作与长期维护。项目地址:[GitHub](https://github.com/WANG-Fan0912/SnowAdmin)。
1020 5
|
3月前
|
开发工具 iOS开发 MacOS
基于Vite7.1+Vue3+Pinia3+ArcoDesign网页版webos后台模板
最新版研发vite7+vue3.5+pinia3+arco-design仿macos/windows风格网页版OS系统Vite-Vue3-WebOS。
434 11
|
2月前
|
JavaScript 安全
vue3使用ts传参教程
Vue 3结合TypeScript实现组件传参,提升类型安全与开发效率。涵盖Props、Emits、v-model双向绑定及useAttrs透传属性,建议明确声明类型,保障代码质量。
305 0
|
4月前
|
缓存 前端开发 大数据
虚拟列表在Vue3中的具体应用场景有哪些?
虚拟列表在 Vue3 中通过仅渲染可视区域内容,显著提升大数据列表性能,适用于 ERP 表格、聊天界面、社交媒体、阅读器、日历及树形结构等场景,结合 `vue-virtual-scroller` 等工具可实现高效滚动与交互体验。
488 1
|
4月前
|
缓存 JavaScript UED
除了循环引用,Vue3还有哪些常见的性能优化技巧?
除了循环引用,Vue3还有哪些常见的性能优化技巧?
291 0
|
5月前
|
JavaScript
vue3循环引用自已实现
当渲染大量数据列表时,使用虚拟列表只渲染可视区域的内容,显著减少 DOM 节点数量。
145 0
|
7月前
|
JavaScript API 容器
Vue 3 中的 nextTick 使用详解与实战案例
Vue 3 中的 nextTick 使用详解与实战案例 在 Vue 3 的日常开发中,我们经常需要在数据变化后等待 DOM 更新完成再执行某些操作。此时,nextTick 就成了一个不可或缺的工具。本文将介绍 nextTick 的基本用法,并通过三个实战案例,展示它在表单验证、弹窗动画、自动聚焦等场景中的实际应用。
645 17
|
7月前
|
JavaScript 前端开发 API
Vue 2 与 Vue 3 的区别:深度对比与迁移指南
Vue.js 是一个用于构建用户界面的渐进式 JavaScript 框架,在过去的几年里,Vue 2 一直是前端开发中的重要工具。而 Vue 3 作为其升级版本,带来了许多显著的改进和新特性。在本文中,我们将深入比较 Vue 2 和 Vue 3 的主要区别,帮助开发者更好地理解这两个版本之间的变化,并提供迁移建议。 1. Vue 3 的新特性概述 Vue 3 引入了许多新特性,使得开发体验更加流畅、灵活。以下是 Vue 3 的一些关键改进: 1.1 Composition API Composition API 是 Vue 3 的核心新特性之一。它改变了 Vue 组件的代码结构,使得逻辑组
1819 0