最近在看element-ui的源码,边学习的同时也在简书上做一个笔记。
首先,看一下el-button的源码
//单独列出来太过麻烦,在这里,我就直接注释在代码上了,便于查看
<template>
<button
class="el-button"
@click="handleClick" //点击事件
:disabled="buttonDisabled || loading" //是否为加载状态
:autofocus="autofocus" //是否默认聚焦
:type="nativeType" //el-button中的自定义样式,primary / success / warning / danger / info / text
:class="[
type ? 'el-button--' + type : '',
buttonSize ? 'el-button--' + buttonSize : '',
{
'is-disabled': buttonDisabled,//是否禁用状态
'is-loading': loading,//是否是加载中
'is-plain': plain,//是否朴素按钮
'is-round': round,//是否圆角
'is-circle': circle//是否圆形按钮
}
]"
>
<i class="el-icon-loading" v-if="loading"></i>
<i :class="icon" v-if="icon && !loading"></i> //icon图标
<span v-if="$slots.default"><slot></slot></span>//插槽,自行百度
</button>
</template>
<script>
export default {
name: 'ElButton',
//此处是定义在公共组件的方法,后面详解provide 和 inject的用法
inject: {
elForm: {
default: ''
},
elFormItem: {
default: ''
}
},
props: {
type: {
type: String,
default: 'default'
},
size: String,
icon: {
type: String,
default: ''
},
nativeType: {
type: String,
default: 'button'
},
loading: Boolean,
disabled: Boolean,
plain: Boolean,
autofocus: Boolean,
round: Boolean,
circle: Boolean
},
computed: {
_elFormItemSize() {
return (this.elFormItem || {}).elFormItemSize;
},
buttonSize() {
return this.size || this._elFormItemSize || (this.$ELEMENT || {}).size;
},
buttonDisabled() {
return this.disabled || (this.elForm || {}).disabled;
}
},
methods: {
//此处是el-button的自定义事件
handleClick(evt) {
this.$emit('click', evt);
}
}
};
</script>
以上是el-button组件的所有内容
接下来时候全局注册
import ElButton from './src/button';
/* istanbul ignore next */
ElButton.install = function(Vue) {
Vue.component(ElButton.name, ElButton);
};
export default ElButton;