Vue(六)插槽(2.6.0+)
插槽在vue2.6.0开始有了新的更新
具名插槽(数据来自父组件)
子组件(定义插槽)这里版本前后没什么变化
<template>
<div>
<header>
<slot name="header"></slot>
</header>
<main>
<slot></slot>
</main>
<footer>
<slot name="footer"></slot>
</footer>
</div>
</template> <script>
export default {
name: "BaseLayout"
}
</script>
父组件(使用)这里版本后废弃了slot="header"这样的写法,在vue3.0后直接移除。取而代之的是v-slot:插槽名称的写法,默认插槽(没有名字,默认为default),缩写为#header
<template>
<base-layout>
<template v-slot:header>
<h1>Header</h1>
</template> <template v-slot:default>
<p>Main</p>
</template> <template v-slot:footer>
<p>Footer</p>
</template>
</base-layout>
</template>
<script>
import BaseLayout from "../components/BaseLayout";
export default {
components: {BaseLayout}
}
</script>
使用缩写#的代码
<template>
<base-layout>
<template #header>
<h1>Header</h1>
</template> <template #default>
<p>Main</p>
</template> <template #footer>
<p>Footer</p>
</template>
</base-layout>
</template>
<script>
import BaseLayout from "../components/BaseLayout";
export default {
components: {BaseLayout}
}
</script>
页面

作用域插槽(数据来自子组件)【2.6.0起,v-slot取代了slot和slot-scope】
子组件(定义了数据并且将数据绑定到特定属性[user]上)
<template>
<div>
<header>
<slot name="header"></slot>
</header>
<main>
<!--绑定在 <slot> 元素上的特性被称为插槽 prop-->
<slot :user="userData">
{{userData.lastName}}
</slot>
</main>
<footer>
<slot name="footer"></slot>
</footer>
</div>
</template> <script>
export default {
name: "BaseLayout",
data: () => ({
userData: {
firstName: 'AAA',
lastName: 'Miss'
}
})
}
</script>
父组件(给 v-slot 带一个值来定义我们提供的插槽 prop 的名字[slotProps])这里废弃了slot="default" slot-scope="slotProps"的写法,直接v-slot:default="slotProps"或者#default="slotProps"
<template>
<base-layout>
<template #header>
<h1>Header</h1>
</template>
<!--slotProps可以使任意名字-->
<template #default="slotProps">
<strong style="color: crimson">{{slotProps.user.firstName}}</strong>
</template> <template #footer>
<p>Footer</p>
</template>
</base-layout>
</template>
<script>
import BaseLayout from "../components/BaseLayout";
export default {
components: {BaseLayout}
}
</script>
数据关系:

页面:

以上代码的意思是,本来这里的后备内容是显示lastName的,我们可以在父组件那里做手脚,让后备内容显示的是firstName。但是无论怎么做,数据都是来自子组件。
将父组件的指定的后备内容注释掉即可显示原生的内容,如下:

页面:

另外一个例子(写一个表格组件,灵感来源于Vuetify框架,没读过它的内部实现,但是一眼能看出使用了插槽,根据我的理解写出下面的例子)
定义:MyTable.vue
<template>
<table>
<slot name="headers">
<tr>
<th v-for="h in headers" :key="h">{{h}}</th>
</tr>
</slot>
<!--这里的插槽prop绑定了当前的数据 :items="i" -->
<slot name="items" v-for="i in items" :items="i">
<tr :key="i[itemKey]" :title="i[itemKey]">
<td v-for="j in i" :key="j">{{j}}</td>
</tr>
</slot>
</table>
</template> <script>
export default {
name: "MyTable",
// 自定义属性
props: ["headers","items","itemKey"],
}
</script> <style scoped>
table{
width: 200px;
margin: 25px auto;
border-collapse: collapse;
border: 1px solid #ddd;
}
table tr,table td{
border-collapse: collapse;
border: 1px solid #ddd;
padding: 8px 10px;
}
</style>
使用:
<template>
<div class="about">
<!-- ********************作用域插槽的应用******************** -->
<!--直接使用-->
<my-table :headers="headersData" :items="itemsData" item-key="id"></my-table>
<!--父组件利用插槽自定义-->
<my-table :headers="headersData" :items="itemsData" item-key="id">
<!--有了作用域插槽,可以由父组件来改变子组件的展示效果-->
<!--在父级作用域中,我们可以给 v-slot 带一个【值】来定义我们提供的插槽 prop 的名字-->
<!--【值】也就是那个 props,可以为任意名字-->
<template v-slot:items="props">
<tr>
<td style="color: darkcyan">{{props.items.id}}</td>
<td style="color: darkcyan">{{props.items.name}}</td>
<td style="color: darkcyan">{{props.items.age}}</td>
</tr>
</template>
</my-table>
<!-- ********************作用域插槽的应用******************** -->
</div>
</template>
<script>
import MyTable from "../components/MyTable"; export default {
name: 'about',
components: {MyTable},
data: () => ({
headersData: ["ID", "Name", "Age"],
itemsData: [
{id: 1, name: 'A', age: 10},
{id: 2, name: 'B', age: 20},
{id: 3, name: 'C', age: 12},
{id: 4, name: 'D', age: 15}
]
})
}
</script>
<style scoped> </style>
页面:(前者直接使用,后者利用作用于插槽改变了颜色)

变量关系

其它
对于默认插槽,我们可以变成更加简洁的写法:
<template v-slot:default="slotProps">
<strong style="color: crimson">{{slotProps.user.firstName}}</strong>
</template>
变成(不带参数的 v-slot 被假定对应默认插槽)
<template v-slot="slotProps">
<strong style="color: crimson">{{slotProps.user.firstName}}</strong>
</template>
对于缩写 #,有限制条件,那就是只能有参数名的情况下才能使用,即:#header=“”。而不带的话会报错,即:#=“”
Vue(六)插槽(2.6.0+)的更多相关文章
- vue.js插槽
		
具体讲解的url https://github.com/cunzaizhuyi/vue-slot-demo //例子 用jsfiddle.net去运行就好 <!DOCTYPE html> ...
 - 基于Vue + Vuex + Vue-router + Webpack 2.0打造微信界面
		
一.项目简介 基于Vue + Vuex + Vue-router + Webpack 2.0打造微信界面,实现了微信聊天.搜索.点赞.通讯录(快速导航).个人中心.模拟对话.朋友圈.设置等功能. 二. ...
 - vue slot 插槽详解
		
插槽含义:就是引入子组件后,在插入子组件元素中添加信息或者标签,使得子组件的指定位置插入信息或者标签 插槽有三种:默认插槽.具名插槽.作用域插槽,由于vue2.6.0后对插槽进行修改,但是兼容2.6. ...
 - 通过UI库深入了解Vue的插槽的使用技巧
		
Vue官网对于插槽的介绍比较简略,插槽本身也比较"烧脑",很容易看晕,我就一直没看懂,直到 使用了element-plus的组件的插槽. 其实我们可以换一个角度来理解插槽,就会豁然 ...
 - vue父子组件(1.0)
		
1.父子组件 在上一篇随笔中展示了vue的组件,当我们继续在组件中写组件,形成组件嵌套的时候,就是我们所说的父子组件了. <!DOCTYPE html> <html lang=&qu ...
 - Vue中插槽指令
		
08.29自我总结 Vue中插槽指令 意义 就是在组件里留着差值方便后续组件内容新增 而且由于插件是写在父级中数据可以直接父级中传输而不需要传子再传父有些情况会减少写代码量 示例 <div id ...
 - [Vue warn]: Duplicate keys detected: '0'. This may cause an update error.
		
1.[Vue warn]: Duplicate keys detected: '0'. This may cause an update error. 第一眼看到这个错误一脸懵逼,项目使用很久了,代码 ...
 - vue 全局插槽 全局插座
		
场景: slot 能够让父组件内容插入到子组件中,但是子孙组件不能够使用slot直接插入内容.在弹窗的时候,全屏弹窗需要直接在组件最上层显示,如果父组件级别不够,弹出就可能不是全屏的. 知识点: 1: ...
 - 细说Vue作用域插槽,匹配应用场景。
		
最近在官方文档中看到,vue新增了一种插槽机制,叫做作用域插槽.要求的版本是2.1.0+. 首先来说一下:顾名思义,所谓作用域插槽,主要就在作用域,需要注意的是(以下几点看不懂不要紧,配合下面的例子, ...
 - [Vue]组件——插槽:slot(匿名插槽,具名插槽)与slot-scope(作用域插槽)
		
1.单个插槽 | 匿名插槽 1.1<navigation-link> 子组件定义为: <a v-bind:href="url" class="nav-l ...
 
随机推荐
- IGC(Interleaved Group Convolutions)
			
深度学习被引起关注是在2012年,用神经网络训练的一个分类模型在ImagNet上取得了第一名,而且其分类精度比第二名高出10多个点,当时所使用的模型为AlexNet,现在看来其为一个比较简单的网络,而 ...
 - Evaluation of fast-convergence algorithm for ICA-based blind source separation of real convolutive mixture
			
实际卷积混合情况下,基于ICA的盲源分离算法快速收敛性能评估[1]. 提出了一种新的盲源分离算法,该算法将独立分量分析ICA和波束形成BF相结合,通过优化算法来解决盲源分离的低收敛问题.该方法由以下三 ...
 - AJax的三种响应
			
AJax的响应 1.普通文本方式(字符串) resp.getWriter().print("你好"); 2.JSON格式当要给前台页面传输 集合或者对象时 使用普通文本传输的时St ...
 - TODO list(咕咕咕。。。
			
1. Segment Tree Beats 2. ZR集训补题(还剩10题) 3. 尽可能将各种数据结构写得熟练...(某神仙:要把平衡树写得跟线段树一样熟练) 4. 树相关数据结构和算法(dsu o ...
 - 前端微信小程序仿菜谱精灵
			
需求描述及交互分析 设计思路和相关知识点 底部标签导航设计 幻灯片轮播效果设计 菜谱专题列表显示设计 菜谱专题详情设计 菜谱分类设计 幻灯片轮播效果动态切换展示一些美食图片 若本号内容有做得不到位的地 ...
 - (16)Go文件处理
			
package main import ( "bufio" "fmt" "os" ) func main() { // 新建文件 file, ...
 - CF1220题解
			
D 考虑从0出发,两个属于集合的元素\(x,y\) \(ax=by\),则形成奇环需要\(a+b\equiv 1(\% 2)\) 需要无奇环,\(\frac{lcm(x,y)}{x}+\frac{lc ...
 - python 安装setuptools、pip《转》
			
https://www.jianshu.com/p/e9ab614cad9b 安装setuptools 下载setuptools源码setuptools-25.2.0.tar.gz 地址:https: ...
 - Android  系统属性-SystemProperties详解***
			
创建与修改android属性用Systemproperties.set(name, value),获取android属性用Systemproperties.get(name),需要注意的是androi ...
 - mysql增删改查sql语句
			
未经允许,禁止转载!!!未经允许,禁止转载!!! 创建表 create table 表名删除表 drop table 表名修改表名 rename table 旧表名 to 新表名字创建数 ...