slot插槽及Element-ui 中<template slot-scope=“scope“>

这篇具有很好参考价值的文章主要介绍了slot插槽及Element-ui 中<template slot-scope=“scope“>。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

slot插槽及Element-ui 中<template slot-scope=“scope“>

一 源码展示

<el-table-column label="状态" align="center" prop="state">
        <template scope="scope">
          <div :style="{'color':scope.row.state==0? 'red':scope.row.state==1? 'orange':scope.row.state==2? 'green':'#333'}">
            {{scope.row.state|stateTrans}}
          </div>
        </template>
      </el-table-column>

slot-scope=“scope” //取到当前单元格
scope.$index //拿到当前行的index
scope.row //拿到当前行的数据对象
scope.row.date //是对象里面的data属性的值

二 slot插槽

插槽有三种:默认插槽、具名插槽、作用域插槽。

2.1 vue的slot默认插槽、具名插槽

<template>
  <div>
    <slot></slot>
  </div>
</template>
<script>
export default {
  name: 'children'
}
</script>
// 使用children组件
 <children>代替slot的内容</children>
<template>
  <div>
    代替slot的内容
  </div>
</template>

假如一个组件里面需要多个插槽。我们怎么来区分多个slot,而且不同slot的显示位置也是有差异的.对于这样的情况, 元素有一个特殊的特性:name。这个特性可以用来定义额外的插槽:
注意:一个不带 name 的 出口会带有隐含的名字“default”。

在向具名插槽提供内容的时候,在元素上通过使用 v-slot 指令,并以 v-slot 的参数的形式提供其名称,将插槽内容放到指定位指:
多个插槽,每个需要<template 包裹

<children>
  <template v-slot:header>
  <!--  <template #header> 具名插槽可缩写形式 -->
    <h1>Here might be a page title</h1>
  </template>

注意:v-slot 只能添加在一个 上 (只有一种例外情况),这一点和已经废弃的 slot特性不同。
例外情况: 当被提供的内容只有默认插槽时,组件的标签才可以被当作插槽的模板来使用。这样我们就可以把 v-slot 直接用在组件上:

2.2作用域插槽

作用域插槽就是实现在子组件自行决定自己要显示什么内容

项目中,插槽的内容中有需要访问到子组件里面的内容,类似子组件里的slot可以绑定一些当前作用域,从而传出来,使用组件时,插槽的内容就可以拿到slot传出来的数据,父级的插槽内容可用。
子组件:

<template>
  <div class="about">
    <h1>This is an 子组件</h1>
    <!-- 子组件中,告诉父组件我要实现obj里面的信息 -->
    //<slot :自定义的name=data中的属性或对象></slot>
    <slot :obj="obj"></slot>
     //动态绑定obj数据
  </div>
</template>

<script>
export default {
  data () {
    return {
      obj: {
        name: 'children'

父组件:

<template>
  <div class="about">
    <h1>This is an Parent page</h1>
    <children v-slot="slotProps">
        <p>one插槽{{slotProps.obj.name}}</p>
        //slotProps.obj.name子组件data中的数据
    </children>
  </div>
</template>
<script>
import Children from './Children.vue'
export default {
  components: {
    Children
  },
  data () {
    return {
    }
  }
}
</script>

子组件有多个作用域插槽时(上边这种简单的写法,因为可能会出现作用域不明确的问题):
子组件:

<template>
  <div class="about">
    <h1>This is an Children page</h1>
    <slot :obj="obj1" name="one"></slot>
    <slot :obj="obj2" name="two"></slot>
  </div>
</template>

<script>
export default {
  data () {
    return {
      obj1: {
        name: 'one slot'
      },
      obj2: {
        name: 'two slot'

父组件:
slotProps是当前插槽bind绑定的所有数据

<template>
  <div class="about">
    <h1>This is an Parent page</h1>
    <children>
        <template v-slot:one="slotProps">
       ***// slotProps是当前插槽bind绑定的所有数据***
          <h2>{{slotProps.obj1.name}}</h2>
        </template>
        <template v-slot:two="twoSlotProps">
          <h2>{{twoSlotProps.obj2.name}}</h2>
        </template>
    </children>
  </div>
</template>

<script>
import Children from './Children.vue'
export default {
  components: {
    Children
  },
  data () {
    return {

2.3 动态插槽名

动态指令参数也可以用在 v-slot 上,来定义动态的插槽名
父组件:

<template>
  <div class="about">
    <h1>This is an Parent page</h1>
    <children>
        <template v-slot:[dynamicSlotName]="slotProps">
          <h2>{{slotProps.obj.name}}</h2>
        </template>
    </children>
  </div>
</template>

<script>
import Children from './Children.vue'
export default {
  components: {
    Children
  },
  data () {
    return {
      dynamicSlotName: 'one'
   


三 element-ui中的slot-scope

3.1 t基础的用法

代码参考 一
基础的用法里面,在el-table中,:data="tableData"是数据集,结构如下:

 <el-table v-loading="loading" :data="gdglList" @selection-change="handleSelectionChange">
      <el-table-column align="center" label="序号"  type="index" width="50"></el-table-column>
      <el-table-column label="单号" align="center" prop="gdglId" v-if="true"/>
      <el-table-column label="委托部门" align="center" prop="deptName" />
      <el-table-column label="项目内容" align="center" prop="itemDescription" />
      

那么对于每一个el-table-column,我们只需要使用prop=“date”,就可以将该列的数据绑定为该数组所有的对象中的“date”属性,我们可以理解为对于tableData,这里始终取的是tableData[$index].date。
table按照tableData这个数组的长度来生成多少行,按照有多少个el-table-column来生成多少列。

3.2 高级的用法

 <el-table v-loading="loading" :data="gdglList" @selection-change="handleSelectionChange">
      <el-table-column align="center" label="序号"  type="index" width="50"></el-table-column>
      <el-table-column label="单号" align="center" prop="gdglId" v-if="true"/>
      <el-table-column label="委托部门" align="center" prop="deptName" />
      <el-table-column label="项目内容" align="center" prop="itemDescription" />
      <el-table-column label="开始时间" align="center" prop="startDatetime" width="180">
             <template slot-scope="scope">
                <span>{{ parseTime(scope.row.startDatetime, '{y}-{m}-{d}') }}</span>
              </template>
      </el-table-column>
      <el-table-column label="预计工时" align="center" prop="estimatedWorkingTime" />
      <el-table-column label="状态" align="center" prop="state">
        <template scope="scope">
					<div :style="{'color':scope.row.state==0? 'red':scope.row.state==1? 'orange':scope.row.state==2? 'green':'#333'}">
						{{scope.row.state|stateTrans}}
					</div>

				</template>
      </el-table-column>
      <el-table-column label="接单人" align="center" prop="projectReceivedBy" />
      <el-table-column label="结束时间" align="center" prop="endDatetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

template(模版) 在这里属于一个固定用法:
我们主要说一下这个scope是个什么东西,按照element上的提示:
通过Scoped slot可以获取到 row, column, $index 和 store(table 内部的状态管理)的数据
我们可以理解为:tableData是给到table的记录集,scope是table内部基于tableData数据生成出来的数据集合(相似且有区别于整个数据)
scope.row.date,我们就可以读取到每一行中的date。文章来源地址https://www.toymoban.com/news/detail-781334.html

到了这里,关于slot插槽及Element-ui 中<template slot-scope=“scope“>的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • [vue]使用Element--Tree 树形控件使用props解决自定义slot-scope=“{ node, data }“的对象问题

     在未用props定义的情况下,slot-scope=\\\"{ node, data }\\\"解析data对象只有data含有lable和children才能识别出内容和子节点 当我获取的数据如下,没有lable和children,使用的是name和childList,如下图所示: 注意props是对象形式,        

    2024年02月11日
    浏览(42)
  • Vue提升:理解vue中的 slot-scope=“scope“

    slot是插槽,slot-scope=“scope“语义更加明确,相当于一行的数据,在实际开发中会碰到如下的场景  这个工作状态是变化的,而我们就可以通过后端返回的具体值来判断这里应该显示什么样的内容,具体代码如下  后端返回消息如下,status状态值为0就是代办,为1就是处置,为

    2024年02月01日
    浏览(24)
  • vue 中slot-scope的作用

    1.slot-scope=\\\"scope\\\"来取得作用域插槽:data绑定的数据,scope可以随便替换其他名称,只是定义对象来代表取得的data数据,用于使用而已。 总的来说就是 scope-slot就是可以用子组件里面的数据, 可以操作子组件里面的数据. 而scope-slot后面接的内容就是个别名, 或者说变量也行, 是指向

    2024年02月16日
    浏览(26)
  • element ui table 表格slot插槽整理

    1、scope.row.字段名获取指定行中指定字段名的数据 以oid为例  编辑删除操作时需要向后端传oid这个字段 2、scope.column.字段名 获取列数据 3、scope.$index 获取当前行的下标 以之前打卡后台管理的项目为例子  后台返回的数据中包含detail这个字段  字段为json字符串 需要根据当前行

    2024年02月16日
    浏览(40)
  • ElementUI的Form表单使用slot-scope=“scope“获取当前表格行数据实现数据回显、修改表单操作

    在写项目时,老师通过向后端发请求获得表格原来的数据来填充修改表单里的数据。 这是表格: 这是点击修改按钮后显示出来的修改表单: 但本地里都已经有这些数据了,就没必要再发一次请求,徒增服务器压力。 准备 可是该怎么获得当前行的数据填充上去呢?答案在

    2023年04月23日
    浏览(38)
  • element-template中的element-ui版本升级后不出现el的代码提示

    在开发基于element-template的vue管理系统中,我需要使用到element-ui的描述列表,但是写入代码后没有相关样式。 经过查询资料后发现可能是由于element-ui的版本较低导致, 于是我更新了element-ui的版本至最新 ,然后就出现了该问题 在idea的vue文件中,打代码的时候没有element-ui的代

    2024年02月15日
    浏览(32)
  • vue中使用element ui的el-table在el-table-column下使用slot插槽v-if条件渲染没生效或者混乱

    vue 引入element ui中的el-table组件时,在el-table-column下使用作用域插槽,通过v-if条件来动态显示某些元素,发现有的条目渲染没生效或者混乱。如: 原因:vue虚拟dom机制,会尽量复用已存在相同节点元素而不会重新渲染,导致使用v-if没有达到预期效果 解决方法:使用div元素将

    2024年02月11日
    浏览(40)
  • 基于vue和element-ui的表格组件,主推数据渲染,支持内容和方法灵活绑定,提供动态具名插槽自定义内容

            组件名为commonTable,主要是基于element-ui中的表格组件进行二次封装的组件,集成了常用的表格功能,除默认内容的显示外,还包括以下几点:         1. 状态的筛选和显示;         2. 操作按钮的显示和方法绑定;         3. 自定义具名插槽内容的封装;      

    2024年02月07日
    浏览(43)
  • 【Vue】Element Plus和Element UI中插槽使用

    今天和大家讲一下Element Plus和Element UI这两个组件库中表格的插槽使用方法,一般情况下vue2使用Element UI这个组件库,表格组件的插槽的话一般都是使用v-slot,而vue3使用Element Plus组件库,表格组件中插槽一般为#default,下面就来讲一下这两个。 Element Plus 和 Element UI 都是基于

    2024年02月12日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包