elementui源码学习——button

一个src文件夹和一个index.js,src文件夹放组件,index.js注册组件并导出

分析从三个方面着手:DOM结构,数据属性,事件

1.DOM结构:

```

<button></button>

```

2.数据属性

1)props获取

2)引用computed的属性

3.事件

这里涉及到父子组件通信,子组件向父组件发消息可以用emit实现,父组件监听即可,一般情况下父组件监听的事件名都是自定义的,这里特殊了点,父组件直接监听了“click”事件,谁让button通常就一个点击事件呢

学习点:

1)样式绑定

```

:class="[

      type ? 'el-button--' + type : '',

      buttonSize ? 'el-button--' + buttonSize : '',

      {

        'is-disabled': buttonDisabled,

        'is-loading': loading,

        'is-plain': plain,

        'is-round': round,

        'is-circle': circle

      }

    ]"

```

class的绑定

绑定的class做了归类,boolean类型的归为了一类,放到对象中,我们是不是也可以这样写,让代码更整齐呢

https://cn.vuejs.org/v2/guide/class-and-style.html#%E6%95%B0%E7%BB%84%E8%AF%AD%E6%B3%95

2)插槽

插槽就是子组件中的提供给父组件使用的一个占位符,用<slot></slot> 表示,父组件可以在这个占位符中填充任何模板代码,如 HTML、组件等,填充的内容会替换子组件的<slot></slot>标签。

```

<span v-if="$slots.default"><slot></slot></span>

```

https://www.cnblogs.com/mandy-dyf/p/11528505.html

https://cn.vuejs.org/v2/api/#vm-slots

附源码

```

<template>

  <button

    class="el-button"

    @click="handleClick"

    :disabled="buttonDisabled || loading"

    :autofocus="autofocus"

    :type="nativeType"

    :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>

    <span v-if="$slots.default"><slot></slot></span>

  </button>

</template>

<script>

  export default {

    name: 'ElButton',

    inject: {

      elForm: {

        default: ''

      },

      elFormItem: {

        default: ''

      }

    },

    props: {

      type: {

        type: String,

        default: 'default'

      },//类型 string primary / success / warning / danger / info / text

      size: String, //尺寸 string medium / small / mini

      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: {

      handleClick(evt) {

        this.$emit('click', evt);

      }

    }

  };

</script>

```

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,514评论 0 13
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,057评论 0 2
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa阅读 8,926评论 0 6
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,452评论 0 10
  • ppmessage支持付费用户使用知识库功能。知识库帮助座席人员回答客户问题。在会话界面直接发送即可。 建立数据库...
    李跃超阅读 587评论 0 1