Vue组件实战之ivew源码Icon篇

本文探讨了iview组件库中Icon的源码,详细解释了Icon组件的用法,包括必填参数type以及可选参数size和color。文章还分享了通过computed计算属性优化组件属性赋值的思想,并简单提到了CSS-in-JS在特定场景下的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源码

<template>
    <i :class="classes" :style="styles"></i>
</template>
<script>
    const prefixCls = 'ivu-icon';

    export default {
        name: 'Icon',
        props: {
            type: String,
            size: [Number, String],
            color: String
        },
        computed: {
            classes () {
                return `${prefixCls} ${prefixCls}-${this.type}`;
            },
            styles () {
                let style = {};

                if (this.size) {
                    style['font-size'] = `${this.size}px`;
                }

                if (this.color) {
                    style.color = this.color;
                }

                return style;
            }
        }
    };
</script>

用法

<Icon type="arrow-resize"></Icon>

<Icon type="arrow-resize" size="18"></Icon>

<Icon type="arrow-resize" color="#333"></Icon>

<Icon type="arrow-resize" size="18" color="#333"></Icon>

其中 type 是必须的,用于指定使用的图标. size, color 是选填的,用于指定使用的大小与颜色.

想法

  • 可以充分的利用 computed 计算属性来给对应的属性赋值.
  • CSS-in-JS 的用法可以用来给定属性赋值.当然这只是应用于少量的情况,在大量使用的情况下,最好还是使用全局CSS.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值