关于vue slot实战体会

       前端在写公共组件的时候,为了实现组件的可扩展性和复用性,通常会使用Slot来进行扩展。避免长篇阔谈,我这里直接上项目实战代码。
      场景:为多个页面写一个公共Table组件,每个页面对于表格的操作都不太一样,这就需要我们在Table组件里面留下Slot,便于其他组件扩展引用。(注:UI用的Ant)。

Table组件如下:

<template>
    <div class='be-table'>
        <a-table bordered :dataSource="dataSource" :columns="columns" :rowKey="rowKey" :pagination="pagination" @change="changeFun" >
            <template slot="operation" slot-scope="text,record">
                <div class="table-row-operations">
                    <slot name="myOperations" :record="record"></slot>
                </div>
            </template>
        </a-table>
    </div>
</template>
<script>
    export default {
        name:'BeTable',
        props:{
            columns:{
                type:Array,
                require: true
            },
            dataSource:{
                type:Array,
                require: true
            },
            rowKey:{
                type:String,
                default: 'name'
            },
            loading:{
              type:Boolean,
              default:false
            },
            pagination:{
                type:Object,

            },
            changeFun:{
                type:Function,
                default: ()=> {

                }
            },
        },
        data() {
            return {
                count: 2,
            };
        },
        methods: {
        },
    };
</script>
<style scoped lang="less">
    @import '~@assets/app.less';
    .be-table{
        /*height: 100%;*/
        width: 100%;
        .editable-add-btn {
            margin-bottom: 8px;
        }
        .table-row-operations{
            color:@baseBG2-color2;
            text-align: center;
            i:hover{
                transform: scale(1.2);
                cursor: pointer;
            }
            span:hover{
                /*font-size: 16px;*/
                cursor: pointer;
                text-decoration:underline;
            }

        }
    }
</style>

组件引用:

这里使用了具名插槽,并将利用作用域将"record"从table组件引出来,便于我们自定义扩展传值。这里只需要在<template>里面加入我们想添加的东西(如:新增、删除、查看、其他自定义操作),简单说就是想写什么就写什么,响应函数就在引用组件里面写。是不是很灵活呢!!!!!

扩展:
(1)查看、删除操作

 <BeTable :columns="columns" :dataSource="dataSource" :rowKey="rowKey">
       <template #myOperations="{record}">
                <a-icon type="info-circle" title="查看" @click="doShowInfo(record[rowKey],record)"/>
                <a-divider type="vertical"/>
                <a-popconfirm title="确定删除?" @confirm="() => doDelete(record[rowKey])" okText="确认" cancelText="取消">
                    <a-icon type="delete" title="删除" />
                </a-popconfirm>
                <a-divider type="vertical"/>
                <a-icon type="pause-circle" v-if=" record['state']=='running'" @click="doPlay(record[rowKey],'pause')" title="暂停"/>
                <a-icon type="play-circle" v-if="record['state']=='paused'"  @click="doPlay(record[rowKey],'start')" title="播放"/>
        </template>
 </BeTable>

(2)自定义其他操作(”逻辑特征类“、”逻辑对象类“)

 <BeTable :columns="columns" :dataSource="dataSource" :rowKey="rowKey">
      <template #myOperations="{record}">
                <span @click="showLgcFtrCls(record)">逻辑特征类</span>
                <a-divider type="vertical" />
                <span @click="showLgcObjCls(record)" >逻辑对象类</span>
                <a-divider type="vertical" />
                <a-popconfirm title="确认删除?"  @confirm="() => doDelete(record[rowKey])" okText="确认" cancelText="取消">
                    <a-icon type="delete" title="删除"/>
                </a-popconfirm>
      </template>
</BeTable>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值