尝试封装基础组件-button

本文介绍了如何基于业务需求封装一个基础的按钮组件,包括组件的API(属性、事件、插槽),并提供了具体的代码实现。示例代码展示了基础按钮的样式和功能,如不同类型的按钮(主要颜色)、禁用状态等。作者提出组件应增加更多功能,如图标、加载效果,并建议初学者逐步提升组件功能。
摘要由CSDN通过智能技术生成

前言

在业务仔的生活中,经常就是使用各种组件去堆页面。使用第三方的组件库,有时候不能满足项目的样式需求,所以需要封装项目的基础组件库,本文所写的就是一个简单的小案例。

基础组件的三个APi

  1. 属性prop
    
  2. 事件event
    
  3. 插槽slot
    

举个例子

image.png

实现基础组件
  1. 明确功能
    
  2. 逻辑处理
    
  3. css样式
    
代码

Demo

<template>
    <div class="base-button-container" :class="[{'disabled':disabled}]" :style="{'margin':margin}">
        <button style="width: 100%;height: 100%" @click="$emit('click')" :class="[type,'base']" :disabled="disabled">
            <span>{{label}}</span>
        </button>
    </div>
</template>

<script>
  export default {
    name: "base-button",
    props:{
        label:{
          type:String,
          default:'确认'
        },
        type:{
          type:String,
          default:'primary'
        },
        disabled:{
            type:Boolean,
            default:false
        },
        margin:{
            type:String,
            default:"5px"
        }
    },
    data(){
      return{

      }
    },

  }
</script>

<style lang="scss" scoped>
    .base-button-container{
        height: 40px;
        width: 98px;
        color: #ffffff;
        font-size: 16px;
        border-radius: 4px;
    }

    .base{
        border-radius: 4px;
        outline: none;
        cursor: pointer;
        text-align: center;
        color: white;
        z-index: 99999;
        border: 1px solid #dcdfe6;

    }

    .disabled{
        background-color:#DCDCDC;
        cursor: not-allowed;
    }

    .primary{
        background-color: $blue-1;
        &:hover{
            background-color: $blue-1;
            border-color: $blue-1;
        }
        &:active{
            background-color: $blue-2;
        }

        &:disabled{
            background-color:#DCDCDC;
            border: 1px solid #ebeef5;
            cursor: not-allowed;
        }
    }

    .danger{
        background-color: $red-1;
        &:hover{
            background-color: $red-1;
            border-color:$red-1;
        }
        &:active{
            background-color: $red-2;
        }

        &:disabled{
            background-color:#DCDCDC;
            border: 1px solid #ebeef5;
            cursor: not-allowed;
        }
    }

    .success{
        background-color: $green-1;
        &:hover{
            background-color: $green-1;
            border-color:$green-1;
        }
        &:active{
            background-color: $green-2;
        }

        &:disabled{
            background-color:#DCDCDC;
            border: 1px solid #ebeef5;
            cursor: not-allowed;
        }
    }
</style>

样式参照elementUi按钮的样式,需要在组件中接收type来指定class

功能设想

目前实现的这个button的Demo涵盖的功能其实并不全面,参照elementUi,button组件中应该存在图标,加载效果。同时为了满足基本需求,也应该加上防抖的操作,避免用户误操作。

总结

业务仔也应该自己动手封装组件,虽然可能封装的组件在功能或者实现的方法上面存在缺陷,但是慢慢来。本文写的只是一个很简单的Demo,对于大佬们可能没有什么帮助,但是如果对一些xdm有那么一丢丢的帮助我也满足了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值