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,并在项目中引入它们。

相关推荐
老神在在0013 小时前
SpringMVC1
java·前端·学习·spring
萌萌哒草头将军5 小时前
🚀🚀🚀React Router 现在支持 SRC 了!!!
javascript·react.js·preact
薛定谔的算法6 小时前
# 从0到1构建React项目:一个仓库展示应用的架构实践
前端·react.js
Tina学编程6 小时前
HTML基础P1 | HTML基本元素
服务器·前端·html
一只小风华~8 小时前
Web前端:JavaScript和CSS实现的基础登录验证功能
前端
90后的晨仔8 小时前
Vue Router 入门指南:从零开始实现前端路由管理
前端·vue.js
LotteChar8 小时前
WebStorm vs VSCode:前端圈的「豆腐脑甜咸之争」
前端·vscode·webstorm
90后的晨仔8 小时前
零基础快速搭建 Vue 3 开发环境(附官方推荐方法)
前端·vue.js
洛_尘8 小时前
Java EE进阶2:前端 HTML+CSS+JavaScript
java·前端·java-ee
孤独的根号_8 小时前
Vite背后的技术原理🚀:为什么选择Vite作为你的前端构建工具💥
前端·vue.js·vite