当前位置:首页 > VUE

vue公共按钮实现

2026-01-16 07:06:29VUE

Vue 公共按钮组件实现方法

封装基础按钮组件

创建 Button.vue 文件,定义基础按钮模板和样式:

<template>
  <button 
    :class="['base-button', type, size, { 'is-disabled': disabled }]"
    :disabled="disabled"
    @click="handleClick"
  >
    <slot></slot>
  </button>
</template>

<script>
export default {
  name: 'BaseButton',
  props: {
    type: {
      type: String,
      default: 'default',
      validator: value => ['default', 'primary', 'danger', 'warning'].includes(value)
    },
    size: {
      type: String,
      default: 'medium',
      validator: value => ['small', 'medium', 'large'].includes(value)
    },
    disabled: Boolean
  },
  methods: {
    handleClick(e) {
      this.$emit('click', e)
    }
  }
}
</script>

<style scoped>
.base-button {
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}
/* 不同类型样式 */
.primary {
  background: #1890ff;
  color: white;
}
/* 尺寸样式 */
.small { padding: 5px 10px; }
.medium { padding: 8px 15px; }
.large { padding: 12px 20px; }
.is-disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
</style>

全局注册组件

main.js 中全局注册:

import Vue from 'vue'
import BaseButton from '@/components/Button.vue'

Vue.component('BaseButton', BaseButton)

组件使用方式

在任意组件中直接使用:

<template>
  <div>
    <base-button type="primary" @click="handleSubmit">提交</base-button>
    <base-button type="danger" size="small" :disabled="isDisabled">删除</base-button>
  </div>
</template>

扩展功能实现

添加图标支持:

<template>
  <button class="base-button">
    <i v-if="icon" :class="['icon', icon]"></i>
    <slot></slot>
  </button>
</template>

<script>
export default {
  props: {
    icon: String
  }
}
</script>

主题定制方案

通过 SCSS 变量实现主题切换:

// variables.scss
$primary-color: #1890ff;
$danger-color: #ff4d4f;

// button.scss
.primary {
  background: $primary-color;
}
.danger {
  background: $danger-color;
}

单元测试示例

使用 Jest 测试组件行为:

import { shallowMount } from '@vue/test-utils'
import BaseButton from '@/components/Button.vue'

describe('BaseButton', () => {
  it('emits click event', () => {
    const wrapper = shallowMount(BaseButton)
    wrapper.trigger('click')
    expect(wrapper.emitted('click')).toBeTruthy()
  })
})

按需加载配置

配合 babel-plugin-component 实现按需引入:

// babel.config.js
module.exports = {
  plugins: [
    [
      'component',
      {
        libraryName: 'element-ui',
        styleLibraryName: 'theme-chalk'
      }
    ]
  ]
}

vue公共按钮实现

标签: 按钮vue
分享给朋友:

相关文章

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue实现查询替换

vue实现查询替换

Vue 实现查询替换功能 在 Vue 中实现查询替换功能,可以通过数据绑定和字符串操作方法结合实现。以下是具体实现方式: 基础实现 <template> <div>…

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue实现主页

vue实现主页

Vue 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…

vue树形实现

vue树形实现

Vue 树形组件实现方法 使用递归组件实现树形结构 递归组件是 Vue 中实现树形结构的常见方法。通过组件调用自身的方式,可以轻松构建多层嵌套的树形结构。 <template> &…

vue实现框架

vue实现框架

Vue 框架实现的核心方法 基础项目搭建 使用 Vue CLI 或 Vite 快速初始化项目。Vue CLI 适合传统项目,Vite 更适合现代轻量级应用。安装后通过命令行工具选择所需配置(如 Rou…