源码
<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.