Vue2按钮组件(Button)

简介: 这篇文章介绍了如何在Vue框架中创建一个可自定义样式和行为的按钮组件,包括按钮的文本、类型、尺寸、宽度、高度、圆角、跳转目标、禁用状态和是否居中显示等属性。

可自定义设置以下属性:

  • 按钮默认文本(name),默认不设置时显示为'按钮'

  • 按钮类型(type),默认'default',另外可选'primary' 'danger'

  • 按钮悬浮变化效果(effect),只有type为default时,reverse才生效

  • 按钮尺寸(size),默认'middle',另外可选'small' 'large'

  • 按钮宽度(width),默认0

  • 按钮高度(height),默认40px

  • 按钮圆角(borderRadius),默认4px

  • 按钮跳转目标URL地址(route),默认{},格式与的to属性一致(只要未设置时才可监听click事件)

  • 按钮如何打开目标URL,设置route时才起作用,默认'_self'

  • 按钮是否禁用(disabled),默认false

  • 是否将按钮设置为块级元素并居中展示(center),默认false

效果如下图:

①创建按钮组件Button.vue:

<template>
  <span :class="['m-button', {'center': center}]">
    <router-link
      v-if="isRoute"
      :to="route"
      :target="target"
      :disabled="disabled"
      class="u-button fade"
      :class="[type, size, {[effect]: type === 'default', widthType: width, disabled: disabled}]"
      :style="{borderRadius: borderRadius + 'px', width: (width - 2) + 'px', height: (height - 2)+'px', lineHeight: (height - 2)+'px'}">
      <slot>{
  
  { name }}</slot>
    </router-link>
    <a
      v-else
      @click="$emit('click')"
      :disabled="disabled"
      class="u-button"
      :class="[type, size, {[effect]: type === 'default', widthType: width, disabled: disabled}]"
      :style="{borderRadius: borderRadius + 'px', width: (width - 2) + 'px', height: (height - 2)+'px', lineHeight: (height - 2)+'px'}">
      <slot>{
  
  { name }}</slot>
    </a>
  </span>
</template>
<script>
export default {
  name: 'Button',
  props: {
    name: { // 按钮默认文本
      type: String,
      default: '按钮'
    },
    type: { // 按钮类型
      type: String,
      default: 'default' // 'default' 'primary' 'danger'
    },
    effect: { // 按钮悬浮变化效果,只有type为default时,reverse才生效
      type: String,
      default: 'fade' //  'fade' 'reverse'
    },
    size: { // 按钮尺寸
      type: String,
      default: 'middle' // 'small' 'middle' 'large'
    },
    width: { // 按钮宽度
      type: Number,
      default: 0
    },
    height: { // 按钮高度
      type: Number,
      default: 40
    },
    borderRadius: { // 按钮圆角
      type: Number,
      default: 4
    },
    route: { // 按钮跳转目标URL地址
      type: Object,
      default: () => {
        return {}
      }
    },
    target: { // 按钮如何打开目标URL,设置route时才起作用
      type: String,
      default: '_self'
    },
    disabled: { // 按钮是否禁用
      type: Boolean,
      default: false
    },
    center: { // 是否将按钮设置为块级元素并居中展示
      type: Boolean,
      default: false
    }
  },
  computed: {
    isRoute () {
      if (JSON.stringify(this.route) === '{}') {
        return false
      } else {
        return true
      }
    }
  }
}
</script>
<style lang="less" scoped>
@primary: #1890ff;
@danger: #ff4d4f;
.m-button {
  display: inline-block;
  .u-button {
    display: inline-block;
    color: rgba(0,0,0,.65);
    background-color: #fff;
    border: 1px solid #d9d9d9;
    box-shadow: 0 2px 0 rgb(0 0 0 / 2%);
    transition: all .3s cubic-bezier(.645,.045,.355,1);
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    cursor: pointer;
  }
  .primary {
    color: #fff;
    background-color: @primary;
    border-color: @primary;
    text-shadow: 0 -1px 0 rgb(0 0 0 / 12%);
    box-shadow: 0 2px 0 rgb(0 0 0 / 5%);
    &:hover {
      background-color: #40a9ff;
      border-color: #40a9ff;
    }
    &:active {
      background-color: #096dd9;
      border-color: #096dd9;
    }
  }
  .default {
    .fade();
  }
  .danger {
    color: #fff;
    background-color: @danger;
    border-color: @danger;
    text-shadow: 0 -1px 0 rgb(0 0 0 / 12%);
    box-shadow: 0 2px 0 rgb(0 0 0 / 5%);
    &:hover {
      background-color: #ff7875;
      border-color: #ff7875;
    }
    &:active {
      background-color: #d9363e;
      border-color: #d9363e;
    }
  }
  .fade {
    &:hover {
      color: #40a9ff;
      border-color: #40a9ff;
    }
    &:active {
      color: #096dd9;
      border-color: #096dd9;
    }
  }
  .reverse {
    &:hover {
      color: #fff;
      background-color: #40a9ff;
      border-color: #40a9ff;
    }
    &:active {
      color: #fff;
      background-color: #096dd9;
      border-color: #096dd9;
    }
  }
  .small {
    height: 24px;
    line-height: 24px;
    padding: 0 7px;
    font-size: 14px;
  }
  .middle {
    height: 32px;
    line-height: 32px;
    padding: 0 15px;
    font-size: 14px;
  }
  .large {
    height: 40px;
    line-height: 40px;
    padding: 0 15px;
    font-size: 16px;
  }
  .widthType {
    padding: 0;
    text-align: center;
  }
  .disabled {
    color: rgba(0, 0, 0, 0.25);
    background-color: #f5f5f5;
    border-color: #d9d9d9;
    text-shadow: none;
    box-shadow: none;
  }
}
.center {
  display: block;
  text-align: center;
}
</style>

②在要使用的页面引入:

<Button
    type="default"
    effect="reverse"
    size="middle"
    :width="120"
    :height="40"
    :borderRadius="4"
    :disabled="false"
    :center="false"
    @click="onClick">
    按钮Button
</Button>
import Button from '@/components/Button'
components: {
    Button,
},
methods: {
    onClick () {
      console.log('click')
    }
}
相关文章
|
4月前
|
JavaScript
【vue】 vue2 自定义指令 实现全屏 、对话框拖拽
【vue】 vue2 自定义指令 实现全屏 、对话框拖拽
184 2
|
4月前
|
JavaScript
Vue给Element UI的el-popconfirm绑定按钮事件
Vue给Element UI的el-popconfirm绑定按钮事件
|
21天前
|
JavaScript
Vue3按钮(Button)
这是一个高度可定制的按钮组件,支持多种属性设置,包括按钮类型、形状、图标、尺寸、背景透明度、波纹颜色、跳转地址、打开方式、禁用状态、加载状态及指示符样式等。预览图展示了不同配置下的按钮样式变化。组件通过Vue实现,并提供了丰富的自定义选项以适应各种场景需求。
Vue3按钮(Button)
|
21天前
Vue3对话框(Dialog)
该 Vue2 对话框组件提供丰富的可定制属性,如标题、内容、宽度、高度等,并支持自定义按钮文本和样式。其预览效果展示了多种使用场景,包括全屏切换、加载状态及自定义样式等。该组件适用于各种需要弹窗功能的应用场景。[在线预览](https://themusecatcher.github.io/vue-amazing-ui/guide/components/dialog.html)提供了更多实例。此文章详情见原文链接,若涉及版权问题,请告知以便删除。
Vue3对话框(Dialog)
|
21天前
Vue2对话框(Dialog)
这是一篇介绍如何在Vue3中使用对话框(Dialog)的文章。该对话框组件可自定义标题、内容、尺寸等属性,并支持全屏切换、加载中状态等功能,整体样式参考了ant-design-vue Modal的设计。文章详细介绍了创建和使用Dialog组件的方法。
Vue2对话框(Dialog)
|
1月前
在 Vue3 + ElementPlus 项目中使用 el-autocomplete 控件
本文介绍了在Vue3 + ElementPlus项目中如何使用`el-autocomplete`控件实现自动补全输入功能,并探讨了不同版本ElementPlus中`clearable`属性的兼容性问题。
180 0
在 Vue3 + ElementPlus 项目中使用 el-autocomplete 控件
|
4月前
|
JavaScript
【vue】 vue2 点击按钮获取按钮上的值
【vue】 vue2 点击按钮获取按钮上的值
33 0
|
4月前
|
JavaScript API
vue element plus Button 按钮
vue element plus Button 按钮
142 0
|
4月前
|
JavaScript
vue中如何点击事件,获取该点击元素
vue中如何点击事件,获取该点击元素
37 0