当前位置: 首页 > news >正文

VUE之插槽

1、默认插槽

<template><div class="father"></div><h3>父组件</h3><div class="content"><Category title="热门游戏列表"><ul><li v-for="g in games" :key="g.id">{{ g.name }}</li></ul></Category><Category title="今日美食城市"><img :src="imgUrl" alt=""></Category><Category title="今日影视推荐"><video :src="videoUrl" controls></video></Category></div>
</template><script lang="ts" name="Father">import Category from './Category.vue'import {ref,reactive} from "vue"let games = reactive([{id:1,name:'英雄联盟'},{id:2,name:'绝地求生'},{id:3,name:'和平精英'},{id:4,name:'王者荣耀'}])let imgUrl = 'https://img2.baidu.com/it/u=3237968337,425972872&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500'let videoUrl = 'http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4'</script><style scoped>
.category {background-color: skyblue;border-radius: 10px;box-shadow: 0 0 10px;padding: 10px;width: 200px;height:300px;
}.content{display: flex;text-align: center;font-size: 18px;font-weight: 800;
}img,video{width: 100%;
}
</style>
<template><div class="category"></div><h2>{{  title }}</h2><slot>默认内容</slot>
</template><script lang="ts" name="Category"></script><style scoped>
.category {background-color: skyblue;border-radius: 10px;box-shadow: 0 0 10px;padding: 10px;width: 200px;height:300px;
}.h2 {background-color: orange;
}
</style>

2、具名插槽

<Category title="热门游戏列表" ><template v-slot:s2><ul><li v-for="g in games" :key="g.id">{{ g.name }}</li></ul></template><!-- <template  v-slot:s1> --><template  #:s1><h2>热门游戏列表</h2></template></Category><template><div class="category"></div><h2>{{  title }}</h2><!-- 具名插槽 --><slot name="s1">默认内容1</slot><slot name="s2">默认内容2</slot>
</template>

3、作用域插槽

<template><div class="father"></div><h3>父组件</h3><div class="content"><Game> <template v-slot="params"><ul><li v-for="g in params.youxi" :key="g.id">{{ g.name }}</li></ul></template></Game><Game><template v-slot="params"><ol><li v-for="g in params.youxi" :key="g.id">{{ g.name }}</li></ol></template></Game><Game> <template v-slot="{youxi}"><h3 v-for="g in youxi" :key="g.id">{{ g.name }}</h3></template></Game></div>
</template>
<template><div class="game"><h2>游戏列表</h2><slot :youxi="games" x="哈哈" y="你好"></slot></div>
</template><script setup lang="ts" name="Game">import {reactive} from 'vue'let games = reactive([{id:'agshshgshhhs01',name:'英雄联盟'},{id:'agshshgshhhs02',name:'绝地求生'},{id:'agshshgshhhs03',name:'和平精英'},{id:'agshshgshhhs04',name:'王者荣耀'}])
</script><style scoped>
.game {width: 200px;height: 300px;background-color: skyblue;border-radius: 10px;box-shadow: 0 0 10px;
}
</style>
组件关系传递方式
父传子props、v-model、$refs、默认插槽、具名插槽
子传父props、自定义事件、v-model、$parent、作用域插槽
祖传孙、孙传祖$attrs、provide、inject
兄弟间、任意组件间mitt、pinia
http://www.lryc.cn/news/532441.html

相关文章:

  • 4. Go结构体使用
  • 版本控制的重要性及 Git 入门
  • [NKU]C++安装环境 VScode
  • deepseek本地部署
  • 网络编程day1
  • QFileDialog::getOpenFileName(this,“文件对话框“,“.“,“c++ files(*.cpp);;“); 文件对话框显示乱码
  • 绿联NAS安装cpolar内网穿透工具实现无公网IP远程访问教程
  • C++学习——缺省参数、重载函数、引用
  • web-JSON Web Token-CTFHub
  • langchain教程-11.RAG管道/多轮对话RAG
  • Postgresql的三种备份方式_postgresql备份
  • WebAssembly:前后端开发的未来利器
  • Mac下使用brew安装go 以及遇到的问题
  • 【Leetcode 每日一题】47. 全排列 II
  • 车型检测7种YOLOV8
  • C语言按位取反【~】详解,含原码反码补码的0基础讲解【原码反码补码严格意义上来说属于计算机组成原理的范畴,不过这也是学好编程初级阶段的必修课】
  • 面对全球化的泼天流量,出海企业如何观测多地域网络质量?
  • 『python爬虫』获取免费IP代理 搭建自己的ip代理池(保姆级图文)
  • 21.命令模式(Command Pattern)
  • 深入探索 C++17 特征变量模板 (xxx_v)
  • 【Day32 LeetCode】动态规划DP Ⅴ 完全背包
  • 景区如何打造高质量游览观光车,提高人流量?
  • 【Ubuntu】ARM交叉编译开发环境解决“没有那个文件或目录”问题
  • 蓝桥杯之c++入门(六)【string(practice)】
  • go的sync包学习
  • 互联网上常见的,ip地址泛播什么意思
  • Linux/C高级(精讲)----shell结构语句、shell数组
  • 14.kafka开机自启动配置
  • 11 享元(Flyweight)模式
  • PHP JSON操作指南