vue2之动态class与动态class组件的封装

vue2之动态class与动态class组件的封装

动态class

方式1::class="isRed ? ‘red’ : ‘’

//CSS
    .red {
      color: red;
    }
//HTML
  <span :class="isRed ? 'red' : '' ">对象呀</span>
//JS
 isRed: true,

方式2 :class=“[‘red’,isShow?‘bg’:‘’]”

    <button @click="isShow=true">点击 显示黄色背景</button>
    <h1 :class="['red',isShow?'bg':'']">我是一个大标签</h1>

      isShow: false

组件之 :class=“[iLabel === ‘成功’ ? ‘success’ : iLabel === ‘失败’ ? ‘fail’: iLabel === ‘备份中’ ? ‘dumping’ : iLabel === ‘失效’ ? ‘lost’ : ‘-’]” 封装组件

base.vue组件

<template>
    <div />
</template>

<script>
export default {
    name: 'BaseFormatter',
    props: {
        row: {
            type: Object,
            default: () => ({})
        },
        column: {
            type: Object,
            default: null
        },
        index: {
            type: Number,
            default: 0
        },
        col: {
            type: Object,
            default: () => ({})
        },
        cellValue: {
            type: [String, Boolean, Number, Object, Array],
            default: null
        }
    },
    data() {
        return {};
    }
};
</script>

BackUpStatus组件

<template>
    <div class="backUp-status">
        <!-- :class="[iLabel === '成功' ? 'success' : 'fail']" -->
        <div
            class="label"
            :class="[iLabel === '成功' ? 'success' : iLabel === '失败' ? 'fail': iLabel === '备份中' ? 'dumping' : iLabel === '失效' ? 'lost' : '-']"
        >
            {{ iLabel }}
        </div>
    </div>
</template>

<script>
import BaseFormatter from './base.vue';
export default {
    name: 'BackUpStatus',
    components: {},
    extends: BaseFormatter,
    props: {
        status: {
            type: Object,
            default: () => {}
        }
    },
    data() {
        return {
        };
    },
    computed: {
        iLabel() {
            // console.log('cellValue', this.cellValue);
            return this.status[this.cellValue];
        }
    },
    methods: {
    }
};

</script>
<style  lang="scss" scoped>
.backUp-status {
	.success {
		color: #5AD8A6;
	}
	.fail {
		color: #D9001B;
	}
	.dumping {
		color: #F9C583;
	}
	.lost {
		color: gray;
	}
}
</style>

使用组件

内嵌表格的
                       formatter: BackUpStatus,
                        status: { success: '成功', fail: '失败'}
  直接使用
  <BackUpStatus :status='status' :cellValue='cellValue'></BackUpStatus>

效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值