首页 > 其他分享 >vue table 里面 slot 的模板复用 slot-scope template v-for

vue table 里面 slot 的模板复用 slot-scope template v-for

时间:2023-04-17 11:39:08浏览次数:64  
标签:slot vue slotType toLine item template scope row

vue table 里面 slot 的模板复用 slot-scope template v-for

需求

经常在table里面要有自定义列,但是会有相同的自定义列,这个时候又不想写很多一样的template,就可以用这种方式

代码

<template :slot="slotName" v-for="slotName in [
      'slotName1',
      'slotName2',
      'slotName3',
      'slotName4'
      ]" slot-scope="{row, index}">
        <AutoTipZen :key="slotName" :iTitle="row[slotName]">{{ toLine(row[slotName]) }}</AutoTipZen>
      </template>

代码转配置

刚才那个数组可以从columns的数组里面获取

<template 
:slot="slotName" 
v-for="slotName in columns.filter(item => item.slotType === 'autoTip').map(item => item.slot)" 
slot-scope="{row, index}">

后记正式代码

<template :slot="slotName"
                v-for="slotName in columns.filter(m => m.slotType === 'textarea').map(m => m.slot)"
                slot-scope="{row, index}">
        <AutoTipZen :key="slotName" :iTitle="row[slotName]">{{ toLine(row[slotName]) }}</AutoTipZen>
      </template>

columns数组

[
  {
    title: 'fieldName',
    slot: 'slotName',
    slotType: 'textarea'
  },
  {
    title: 'fieldName2',
    slot: 'slotName2',
    slotType: 'textarea'
  },
]

函数

toLine (txt) {
      return txt.replace(/\r/g, '').replace(/\n/g, '')
    },

总结

原理基本上是这个意思,就拆分了业务逻辑,并且可以组合使用。

---------------------------------------------
生活的意义并不是与他人争高下,而在于享受努力实现目标的过程,结果是对自己行动的嘉奖。
↑面的话,越看越不痛快,应该这么说:

生活的意义就是你自己知道你要做什么,明确目标。没有目标,后面都是瞎扯!

新博客 https://www.VuejsDev.com 用于梳理知识点



标签:slot,vue,slotType,toLine,item,template,scope,row
From: https://blog.51cto.com/u_15770151/6194840

相关文章

  • Vue中单选框或复选框中的label内容过长,超出范围
     解决办法:1、直接把字体调小2、当需要的字体大小还是超出了范围时在css中写(如果是复选框;单选框的class:el-radio__label).el-checkbox__label{text-overflow:ellipsis;white-space:normal;line-height:18px;......
  • vite启动vue项目报错import { performance } from 'node:perf_hooks'
    import{performance}from'node:perf_hooks'^^^^^^SyntaxError:Cannotuseimportstatementoutsideamodule要求node版本要大于16 使用nvm切换node版本 成功运行......
  • vue中el-checkbox全选、反选、多选
    <template><div><el-checkboxv-model="checkAll"@change="handleCheckAllChange":indeterminate="isIndeterminate">全选</el-checkbox><el-checkboxv-model="c......
  • vue input每次输入一个字符后自动失去焦点 问题
    我在输入框输入的时候,每输入一次,输入框就自动失去焦点了。ps:实现的功能是,一个列表组件,每个对象中都有一个input输入框。<divclass="addTags"v-for="(item,index)inlist":key="item.title"><inputtype="text"v-model="item.title"></div>原因问......
  • C++-template class-模板类
    C++-templateclass-模板类【C++高级教程,C++类模板一次讲透,必须收藏!】https://www.bilibili.com/video/BV1v84y1x7Qp/?share_source=copy_web&vd_source=3809390a14c335e7731c9e076c03eeba类模板概念类模板是用于生成类的模板。在编译阶段,编译器会根据类模板的使用情况创建......
  • 在Vue中,关于require与required
    在Vue里面,是用require还是required?始终拿捏不定,所以这里记录一下图片是require('@/xxx')props里面是required表单验证里面是required总结:只有图片是require,其它都是required......
  • vue 项目npm run dev(启动)时报错The service was stopped
    vue项目yarnupgrade后vitebuild报错,如何项目也运行不起来了。报错截图:解决办法:删除node_modules文件夹,然后执行yarninstall重新生成心的node_modules。......
  • template标签的学习
    template标签我在引用这个标签的时候,本来还在纳闷,咋就他那么特殊,就他不显示,然后突然意识到,这个标签天生不可见,即display:none属性可以在template标签里面放content模块,然后使用button点击事件实现内容的展现;具体实现如下:<template><ul><li>模块1</li><......
  • 复选框数据通过axios和Vue传输到servlet后台
    引言本来是想百度一下如何将table标签里面嵌入的复选框通过axios以及Vue传输到Servlet后台;百度之后才发现,是我草率了,原来可以直接用复选框标签进行传输(好吧,又暴露了自己基础不扎实的事实......)相关实现步骤1、将相关的标签el-checkbox引入进去这里我们需要引入三个带有el的标......
  • vue2源码-六、根据render函数生成vnode
    根据render函数生成vnode上文介绍上面已经将模板编译成了render函数,下面就要使用render函数,从而完成渲染的操作:首先,根据render函数生成虚拟节点;然后根据虚拟节点+真实数据生成真实节点。实现mountComponent方法,完成渲染虚拟节点生成封装vm._render方法。Vue.proto......