element-ui button 仿写 demo

基于上篇 button 源码分享写了一个简单 demo,在写 demo 的过程中,又发现了一个小细节,分享一下:

1、组件部分:

<template>
  <button
    class="yss-button"
    @click="handleClick"
    :class="[
      type ? 'yss-button--' + type : '',
      size ? 'yss-button--' + size : ''
    ]"
  >
    <span v-if="$slots.default"><slot></slot></span>
  </button>
</template>
<script>
export default {
  name: 'YssButton',
  props: {
    type: {
      type: String,
      default: 'default'
    },
    size: String,
    icon: {
      type: String,
      default: ''
    },
    circle: Boolean
  },

  methods: {
    handleClick(evt) {
      this.$emit('click', evt);
    }
  }
};
</script>

<style scoped>
.yss-button {
  display: inline-block;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  background: #fff;
  border: 1px solid #dcdfe6;
  color: #606266;
  -webkit-appearance: none;
  text-align: center;
  box-sizing: border-box;
  outline: none;
  margin: 0;
  transition: 0.1s;
  font-weight: 500;
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  padding: 12px 20px;
  font-size: 14px;
  border-radius: 4px;
}

.yss-button--primary {
  color: #fff;
  background-color: #409eff;
  border-color: #409eff;
}

.yss-button--success {
  color: #fff;
  background-color: #67c23a;
  border-color: #67c23a;
}

.yss-button--warning {
  color: #fff;
  background-color: #e6a23c;
  border-color: #e6a23c;
}

.yss-button.is-circle {
  border-radius: 50%;
  padding: 12px;
}
</style>

2、使用部分

<template>
  <div class="demo">
    <yss-button>默认按钮</yss-button>
    <yss-button type="primary">主要按钮</yss-button>
    <yss-button type="success">成功按钮</yss-button>
  </div>
</template>

<script>
export default {
  name: 'demo',
  methods: {
    handleClick() {
      console.log('test...');
    }
  }
};
</script>

3、发现的小问题:如果按我常有的思维逻辑,我会再增加一个属性来控制挂载内容是否展示,而源码当中的使用 $slots.default 这种方式来控制是否展示挂载内容就很好,很完美的少传了一个属性。

4、页面效果

总结:学完一个组件的源码之后,在学习的过程中,会尝试找出它的每个属性,每个方法的一个用途,但过于探索它的用途之后,有时会陷入为研究源码而研究源码的小陷阱,忘记将它带入自己的工作场景进行比对,从而错过了感受它真正的美。如果在研究完源码之后,尝试使用自己的思维方式去写一下试试,哪怕只是把源码改吧改吧跑成功呢,也能在这个过程中有一个全新的收获。

相关推荐

  1. 仿Vue的{{}}语法

    2024-02-02 11:34:03       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-02 11:34:03       91 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-02 11:34:03       97 阅读
  3. 在Django里面运行非项目文件

    2024-02-02 11:34:03       78 阅读
  4. Python语言-面向对象

    2024-02-02 11:34:03       88 阅读

热门阅读

  1. Power BI 不重复计数函数

    2024-02-02 11:34:03       63 阅读
  2. C语言结构体赋值的四种方式

    2024-02-02 11:34:03       62 阅读
  3. 使用Eigen3计算旋转平移缩放矩阵

    2024-02-02 11:34:03       53 阅读
  4. 【Node系列】创建第一个服务器应用

    2024-02-02 11:34:03       54 阅读
  5. day9笔记

    2024-02-02 11:34:03       54 阅读
  6. Golang防止注入常用方法

    2024-02-02 11:34:03       48 阅读
  7. SpringBoot整理-微服务

    2024-02-02 11:34:03       46 阅读
  8. 为什么golang不支持可重入锁呢?

    2024-02-02 11:34:03       53 阅读
  9. 爬虫学习:下厨房的菜谱搜索

    2024-02-02 11:34:03       52 阅读
  10. listagg、xmlagg、group_concat()函数用法

    2024-02-02 11:34:03       58 阅读