vue模拟el-table演示插槽用法

很多人知道插槽分为三种,但是实际到elementui当中为什么这么用,就一脸懵逼,接下来就跟大家聊一聊插槽在elementui中的应用,并且自己写一个类似el-table的组件

vue的slot分为三种::匿名插槽,具名插槽, 作用域插槽,主要作用:让父组件可以向子组件指定位置插入 html 结构,也是一种组件间通信的方式,适用于父组件=>子组件

1、匿名插槽

匿名组件相当于一个占位符,将父组件的数据传入子组件的slot标签内

父组件:

<template>
<ChildSlot>父组件调用</ChildSlot>
</template> <script>
import ChildSlot from "@/components/ChildSlot";
export default {
components:{
ChildSlot
}
}
</script>

子组件:

<template>
<h1>我是子组件</h1>
<slot></slot>
</template> <script>
export default {
name: "ChildSlot"
}
</script>

运行结果如下:

此时父组件中的“父组件调用”这段内容就传递到了子组件,并填入了slot挖的坑当中

2、具名插槽

具名插槽相当于给插槽添加了一个名字(给插槽加入name属性就是具名插槽)

父组件:

<template>
<child-slot>
<template v-slot:username>我是父组件传递的用户姓名</template>
</child-slot>
<child-slot>
<template v-slot:age>我是父组件传递的年龄</template>
</child-slot>
</template> <script>
import ChildSlot from "@/components/ChildSlot";
export default {
components:{
ChildSlot
}
}
</script>

子组件:

<template>
<h1>我是子组件</h1>
<slot name="username"></slot>
<slot name="age"></slot>
</template> <script>
export default {
name: "ChildSlot"
}
</script>

运行结果如下:

此时父组件中的根据slot的name,将内容填入了slot挖的坑当中,一个萝卜一个坑

3、作用域插槽

与前两者的不同 slot自定义:name="值" 子组件可向父组件传递信息

父组件:

<template>
<child-slot>
<template v-slot="{username}">我是子组件传递的用户姓名:{{username}}</template>
</child-slot>
</template> <script>
import ChildSlot from "@/components/ChildSlot";
export default {
components:{
ChildSlot
}
}
</script>

子组件:

<template>
<h1>我是子组件</h1>
<slot :username="username"></slot>
</template> <script>
export default {
name: "ChildSlot",
data(){
return{
username:'java',
}
}
}
</script>

运行结果如下:

通过作用域插槽,我们可以将子组件中的值传入到父组件,在父组件进行数据处理后,填坑到子组件

4、模拟写一个el-table

先看一个el-table的例子,当需要对一行中的某一个单元格的内容进行处理的时候,需要用到slot插槽,例如下面的姓名name的处理

<template>
<el-table
:data="tableData"
style="width: 100%">
<el-table-column
label="姓名"
width="180">
<template slot-scope="scope">
<el-popover trigger="hover" placement="top">
<p>姓名: {{ scope.row.name }}</p>
<p>住址: {{ scope.row.address }}</p>
<div slot="reference" class="name-wrapper">
<el-tag size="medium">{{ scope.row.name }}</el-tag>
</div>
</el-popover>
</template>
</el-table-column>
<el-table-column
prop="address"
label="地址">
</el-table-column>
</el-table>
</template>

参照el-table,实现我们自己的table组件,讲解下为什么需要用插槽,用了哪些插槽

4.1为了传递table,首先通过匿名插槽,写一个的组件,目的就是模拟下面这三行内容

​ <el-table

​ :data="tableData"

​ style="width: 100%">

<template>
<div>
<slot></slot>
</div>
</template> <script>
export default {
name: "MyTable"
}
</script>
4.2实现el-table-column,通过作用域插槽,写我们自己的el-table-column
<template>
<div>
<div>
<!--通过传递label标签,展示表头-->
<span v-if="label">{{ label }}</span>
</div>
<!--获取主页面的data值:$parent.$parent.$data.tableList-->
<div v-for="(user,index) in $parent.$parent.$data.tableList" :key="index">
<!--当传递prop属性的时候,就取user用户的数据-->
<div v-if="prop">{{user[prop]}}</div>
<!--当不传递prop属性的时候,将用户的数据通过row属性,传递到父组件当中,也就是app.vue-->
<slot v-else :row="user"></slot>
</div>
</div>
</template> <script>
export default {
name: "MyTableColumn",
props: {
prop: {type: String},
label: {type: String}
}
}
</script>
4.2调用my-table,my-table-column

我们通过my-table标签,将内容my-table和my-table-column放置到my-table的匿名插槽中,并通过子组件的props属性,接收prop和label。如同elementui一样,如果prop为空,子附件将父组件的user通过作用域插槽传递到父组件,并在父组件进行处理

<template>
<div>
<my-table :data="tableList" style="display: flex; flex-direction: row;">
<my-table-column prop="name" label="姓名"></my-table-column>
<my-table-column prop="sex" label="性别"></my-table-column>
<my-table-column label="地址">
<template v-slot="scope">
<span style="background-color: deepskyblue">{{scope.row.address}}</span>
</template>
</my-table-column>
</my-table>
</div>
</template> <script>
import MyTable from "@/components/MyTable";
import MyTableColumn from "@/components/MyTableColumn";
export default {
name: 'App',
components:{
MyTableColumn,
MyTable
},
data(){
return{
tableList:[
{
name: 'java大师1',
sex: '男',
address: '西藏1'
},
{
name: 'java大师2',
sex: '男',
address: '西藏2'
},
{
name: 'java大师3',
sex: '男',
address: '西藏3'
},
{
name: 'java大师4',
sex: '男',
address: '西藏4'
}
]
}
}
}
</script>

展示结果如下:

vue模拟el-table演示插槽用法的更多相关文章

  1. vue插槽用法(极客时间Vue视频笔记)

    vue插槽 插槽是用来传递复杂的内容,类似方法 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  2. 使用vue模拟购物车小球动画

    使用vue模拟购物车小球动画 1.效果演示 2.相关代码 <!DOCTYPE html> <html lang="en"> <head> < ...

  3. Vue基础01vue的基本示例,vue的双向数据绑定,vue中常见的几种用法,vue相关常见指令

    自学vue框架,每天记录重要的知识点,与大家分享!有不足之处,希望大家指正. 本篇将讲述:vue的基本示例,vue的双向数据绑定,vue中常见的几种用法,vue相关常见指令 前期学习基础,使用vue. ...

  4. Azure Table storage 基本用法 -- Azure Storage 之 Table

    Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table,其中的 Table 就是本文的主角 Azure Tabl ...

  5. EL表达式 requestScope initParam用法

    EL表达式 requestScope initParam用法: servlet: package com.stono.servlet; import java.io.IOException; impo ...

  6. Lua表(table)的用法_个人总结

    Lua表(table)的用法_个人总结 1.表的创建及表的介绍 --table 是lua的一种数据结构用来帮助我们创建不同的数据类型.如:数组和字典--lua table 使用关联型数组,你可以用任意 ...

  7. 使用VUE实现在table中文字信息超过5个隐藏,鼠标移到时弹窗显示全部

    使用VUE实现在table中文字信息超过5个隐藏,鼠标移到时弹窗显示全部 <template> <div> <table> <tr v-for="i ...

  8. table的各种用法

    使用 colgroup 和 col 实现响应式表格(table的各种用法):http://coderlt.coding.me/2017/11/20/table-colgroup/

  9. Vue+element ui table 导出到excel

    需求: Vue+element UI table下的根据搜索条件导出当前所有数据 参考: https://blog.csdn.net/u010427666/article/details/792081 ...

  10. Vue. 之 Element table 单元格内容隐藏

    Vue. 之 Element table 单元格内容隐藏 在table显示数据时,若某个单元格的内容过多,需要进行隐层,在这一列的单元格属性添加::show-overflow-tooltip=&quo ...

随机推荐

  1. C#MD5加密的两种方式

    在开发过程当中,我们经常会用到MD5加密,下面介绍MD5加密的两种方式: /// <summary> /// MD5字符串加密 /// </summary> /// <p ...

  2. 基于Admin.NET框架的前端的一些改进和代码生成处理(2)

    在上篇随笔<基于Admin.NET框架的前端的一些改进和代码生成处理(1)>中大致介绍了一些关于对Admin.NET框架的前端的改造工作,主要目的就是希望能够增加前端代码的简洁性和可读性, ...

  3. 基于el-input实现数字区间输入框(已发布npm/github)

    项目地址:https://github.com/heyu3913/InputNumberRange  (求star) input-number-range tips:更多定制化需求请联系: 13102 ...

  4. NEFUOJ P903字符串去星问题

    Description 有一个字符串(长度小于100),要统计其中有多少个,并输出该字符串去掉后的新字符串. Input 输入数据有多组,每组1个连续的字符串; Output 在1行内输出该串内有多少 ...

  5. 快速使用ChatGpt Web Server

    快速使用ChatGpt Web Server ChatGpt Web Server是使用Blazor Server模式部署的一个服务,所有的逻辑和代码执行都会在服务器执行,然后通过SignalR传输到 ...

  6. 使用 LoRA 和 Hugging Face 高效训练大语言模型

    在本文中,我们将展示如何使用 大语言模型低秩适配 (Low-Rank Adaptation of Large Language Models,LoRA) 技术在单 GPU 上微调 110 亿参数的 F ...

  7. 【Java SE】IO流

    1.File类 ①File类的一个对象代表一个文件或一个文件目录 ②File类声明在java.io下 1.1 FIle类的声明 路径分隔符 Windows和DOS系统默认使用'',UNIX和URL使用 ...

  8. 帝国cms随机sql语句,mysql高效的随机查询

    select * from AppleStorewhere rand()<0.015limit 100;

  9. graphhopper-ios 编译过程详解

    一.写在前面 GraphHopper 是一个快速且高效的路径规划引擎,它默认使用OpenStreetMap和GTFS数据, 也可以导入其他数据源.它可以用作java库或独立的web服务器,去计算两个或 ...

  10. ArrayList实现原理和自动扩容

    ArrayList在Java集合中的位置, ArrayList原理: transient Object[] elementData; ArrayList通过数组来实现. 默认构造方法会构造一个容量为1 ...