Element --Button

Element 代码研读之 Button

Element --Button

工作之余,忽然想好好读下Element代码,这里把自己的理解写下

以下是源码

<template>
  <button
    class="el-button"
    @click="handleClick"
    :disabled="buttonDisabled || loading"	//这里时因为 disabled 和 loading 状态下都不可点击
    :autofocus="autofocus"
    :type="nativeType"
    :class="[	//这里是 绑定class 的典型用法: 1:class需要拼接 写在Array中; 2单纯的has/no 写在 Object中
      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>
    // 这里 v-if="$slots.default" 还不是很理解
    <span v-if="$slots.default"><slot></slot></span>
  </button>
</template>
<script>
  export default {
    name: 'ElButton',
	/*
	myIdea
	inject:
		这里是为了兼容Button用在from 表单的情况,在
			<el-from disabled size="small"></el-from> 
			<el-from-item size="small"></el-from-item>
		上设置 disabled / size 属性,也可以传递到 <el-button></el-button>
		在computed中 buttonSize / buttonDisabled 变量就是这个作用
	*/
    inject: {
      elForm: {
        default: ''
      },
      elFormItem: {
        default: ''
      }
    },

    props: {
      type: {
        type: String,
        default: 'default'
      },
      size: String,	//不传入 size时,取 undefined
      icon: {
        type: String,
        default: ''
      },
      nativeType: {
        type: String,
        default: 'button'
      },
      loading: Boolean,// 不传入loading 时,取 false
      disabled: Boolean,
      plain: Boolean,
      autofocus: Boolean,
      round: Boolean,
      circle: Boolean
    },

    computed: {
      _elFormItemSize() {
      /*
      myIdea
      这里主要是为了兼容 没有在 form表单中使用的情况
	 */
        return (this.elFormItem || {}).elFormItemSize;
      },
      buttonSize() {
        return this.size || this._elFormItemSize || (this.$ELEMENT || {}).size;
      },
      buttonDisabled() {
        return this.disabled || (this.elForm || {}).disabled;
      }
    },
   /*
      myIdea
      下面我简单 写了下computed的变量,自任为更好理解把
	 */
      	_elFormItem() {	// 这里拿到 FormItem 对象
           return this.elFormItem || {}
       },
       _elForm() { // 这里拿到 Form 对象
           return this.elForm || {}
       },
       _disabled() {
           return this.disabled || this._elForm.disabled
       },
       _size() {	
       	//这里 this._elFormItem.size 是有问题的,因为 FormItem 组件也要接收 Form 传下来的 size 属性,所以  [size] 应该是  FormItem 中 size的computed 值
           return this.size || this._elFormItem.size || this._elForm.size
       },
    methods: {
      handleClick(evt) {	//这里抛出 click 方法
        this.$emit('click', evt);
      }
    }
  };
</script>

感悟

上面就是 我对于该段代码的理解,再结合css,就是完整的 Button组件了。
说到 css,这里感觉 sass/less 等css扩展语言还是很必要的,否则会有大量的重复代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值