当前位置:首页 > 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)

组件使用方式

在任意组件中直接使用:

vue公共按钮实现

<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 变量实现主题切换:

vue公共按钮实现

// 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中实现文章锚点定位可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML锚点 通过HTML的id属性和<a>标签的href属性实现锚点跳转。在Vu…

vue实现图库

vue实现图库

Vue 实现图库的基本方法 使用 Vue 实现图库功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用 Vue 和第三方库(如 Vue Gallery) 安装 vue-gallery 或…

vue实现新闻

vue实现新闻

Vue 实现新闻功能 使用 Vue 实现新闻功能通常涉及新闻列表展示、新闻详情页、分类筛选等模块。以下是实现的基本思路和代码示例。 新闻列表展示 通过 Vue 组件展示新闻列表,通常使用 v-for…

vue实现详情

vue实现详情

Vue 实现详情页的常见方法 使用动态路由 在 Vue Router 中配置动态路由参数,通过 $route.params 获取详情 ID。示例路由配置: { path: '/detail/:i…

vue实现github

vue实现github

Vue 实现 GitHub 相关功能 创建 GitHub API 请求 在 Vue 项目中,可以使用 axios 或 fetch 与 GitHub API 进行交互。需要在 GitHub 上创建个人访…

vue底部实现

vue底部实现

Vue 底部实现方法 在 Vue 项目中实现底部布局可以通过多种方式完成,以下是一些常见的方法: 使用固定定位 将底部元素固定在页面底部,适用于单页应用或需要始终显示的底部栏。 <temp…