模仿 element-UI 搭建 Vue UI组件库(二)

封装 button 组件

前置知识:
  • 组件通讯
  • 组件插槽
  • props 校验

参数支持:

参数名 参数描述 参数类型 默认值
type 按钮类型(primary/success/warning/danger/info) string default
plain 是否是朴素按钮 boolean false
round 是否是圆角按钮 boolean false
circle 是否是圆形按钮 boolean false
disabled 是否禁用按钮 boolean false
icon 图标类名 string

事件支持:

事件名 事件描述
click 点击事件

使用插槽:

直白的说,凡是组件中内容可以动态设置的地方,都需要用到slot插槽来自定义内容。

使用 slot 来定义按钮上的文本内容:

<template>
  <button class="wyh-button">
   <span>
       <slot></slot>
    </span>
  </button>
</template>

在使用时就可以直接输入相关内容,即可定义按钮文本内容:

<template>
  <div>
    <wyh-button>登录</wyh-button>
  </div>
</template>

设置自己喜欢的 button 样式,这里 css 样式就不做过多介绍。

type 参数的实现

让按钮支持 type 属性,使得按钮呈现不同的样式:

  1. 父组件传递 type 属性
<template>
  <div id="app">
    <one-button type="primary">primary 按钮</one-button>
  </div>
</template>
  1. 子组件接收 type,限制 type 的值必须为 String 类型并设置默认值
export default {
   
  name: 'oneButton',
  props: {
   
    type:{
   
  	  // 此时对 props 进行校验,值接收 String 类型的 type 值
      type: String,
      // 设置默认值:如果不传值,那么使用 default
      default: 'default'
    }
  }
}
  1. 动态绑定类名控制样式,定好样式前缀,通过传过来的 type 动态绑定类名,用不同的样式。
<template>
  <button class="wyh-button" :class="[`wyh-button-${type}`]">
   <span><slot></slot></span>
  </button>
</template>
  1. 设置不同的样式,例如:
.wyh-button-primary {
   
  color: #fff;
  background-color
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值