vue el-button 封装及使用

使用了 Element UI 中的 el-button 组件,并对其进行了封装和定制。

创建组件index.vue (src/common-ui/button/index.vue)
vue 复制代码
<template>
  <el-button
      class="h-button"
      :type="type"
      :icon="hIcon"
      :disabled="disabled"
      @click="$emit('click')"
      :loading="loading"
  >
    <span class="h-button-caption">{{ hCaption }}</span></el-button
  >
</template>

<script>
  export default {
    name: 'HButton',
    props: {
      action: {
        type: String,
        validator (value) {
          return (
              [
                'add',
                'edit',
                'delete',
                'export',
                'print',
                'return',
                'entry',
                'addNoti',
                'download',
                'appointRow'
              ].indexOf(value) !== -1
          )
        },
        defautl: 'add'
      },
      type: {
        type: String,
        default: 'primary'
      },
      btnClass: String,
      caption: String,
      icon: String,
      disabled: {
        type: Boolean,
        default: false
      },
      loading: {
        type: Boolean,
        default: false
      }
    },
    data () {
      return {
        hCaption: '',
        hIcon: ''
      }
    },
    created () {
      switch (this.action) {
        case 'add':
          this.hCaption = '新增'
          this.hIcon = ''
          break
        case 'edit':
          this.hCaption = '编辑'
          this.hIcon = ''
          break
        case 'delete':
          this.hCaption = '删除'
          this.hIcon = ''
          break
        case 'export':
          this.hCaption = '导出'
          this.hIcon = ''
          break
        default:
          this.hCaption = this.caption
          this.hIcon = this.icon
          break
      }
      if (this.caption) {
        this.hCaption = this.caption
      }
    },
    computed: {
      hasImg () {
        return (
            this.action === 'export' ||
            this.action === 'add' ||
            this.action === 'edit' ||
            this.action === 'delete' ||
            this.action === 'print' ||
            this.action === 'return' ||
            this.action === 'entry' ||
            this.action === 'addNoti'
        )
      },
      isLang () {
        return (
            this.hCaption &&
            (this.hCaption.length > 4 ||
                (this.hCaption.length > 3 && (this.hIcon || this.hasImg)))
        )
      }
    },
    watch: {
      btnClass: {
        handler (val) {
        },
        immediate: true
      }
    }
  }
</script>
页面引入
  • 在需要使用addressCascader组件的地方,通过import语句引入组件注册并使用
vue 复制代码
<template>
  <div>
    <template
        v-for="(
        { caption, display, permissionKey, icon, disabled, callback, action, type, btnClass, loading },
        index
      ) in buttonList"
    >
      <h-button v-if="getButtonDisplay(display)"
                :key="action ? index + action : index"
                :action="action"
                :btnClass="btnClass"
                :caption="caption"
                :icon="icon"
                :disabled="disabled"
                :type="type || 'primary'"
                :loading="loading"
                @click="callback"
                v-permission="permissionKey"
      ></h-button>
    </template>
  </div>
</template>
<script>
  import HButton from "@/common-ui/button/index";

  export default {
    components: {
      HButton
    },
    data() {
      return {
        
        dataSource: [],
        selectedValue: ''
      }
    },
    computed:{
      buttonList() {
        return [{
          caption: "返回",
          type: "warning",
          callback: this.back,
          btnClass: "warningButton"
        },
          {
            caption: "确认",
            type: "primary",
            callback: this.submit,
            btnClass: "primaryButton"
          }]
      }
    },
    methods: {
      back() {},
      submit() {}
    }
    // ...
  }
</script>

确保你已经安装了Vue.js和Element UI,并在项目中引入它们。

相关推荐
d***93527 分钟前
springboot3.X 无法解析parameter参数问题
android·前端·后端
n***84072 小时前
十七:Spring Boot依赖 (2)-- spring-boot-starter-web 依赖详解
前端·spring boot·后端
likuolei6 小时前
XSL-FO 软件
java·开发语言·前端·数据库
正一品程序员6 小时前
vue项目引入GoogleMap API进行网格区域圈选
前端·javascript·vue.js
j***89466 小时前
spring-boot-starter和spring-boot-starter-web的关联
前端
star_11126 小时前
Jenkins+nginx部署前端vue项目
前端·vue.js·jenkins
im_AMBER6 小时前
Canvas架构手记 05 鼠标事件监听 | 原生事件封装 | ctx 结构化对象
前端·笔记·学习·架构
JIngJaneIL6 小时前
农产品电商|基于SprinBoot+vue的农产品电商系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·毕设·农产品电商系统
Tongfront6 小时前
前端通用submit方法
开发语言·前端·javascript·react
c***72746 小时前
SpringBoot + vue 管理系统
vue.js·spring boot·后端