最近在看element-ui的源碼寒匙,邊學(xué)習(xí)的同時也在簡書上做一個筆記。
首先,看一下el-button的源碼
//單獨(dú)列出來太過麻煩批幌,在這里翎猛,我就直接注釋在代碼上了,便于查看
<template>
<button
class="el-button"
@click="handleClick" //點(diǎn)擊事件
:disabled="buttonDisabled || loading" //是否為加載狀態(tài)
:autofocus="autofocus" //是否默認(rèn)聚焦
:type="nativeType" //el-button中的自定義樣式臼节,primary / success / warning / danger / info / text
:class="[
type ? 'el-button--' + type : '',
buttonSize ? 'el-button--' + buttonSize : '',
{
'is-disabled': buttonDisabled,//是否禁用狀態(tài)
'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圖標(biāo)
<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組件的所有內(nèi)容
接下來時候全局注冊
import ElButton from './src/button';
/* istanbul ignore next */
ElButton.install = function(Vue) {
Vue.component(ElButton.name, ElButton);
};
export default ElButton;