vue el-button 封装及使用

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

创建组件index.vue (src/common-ui/button/index.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语句引入组件注册并使用

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

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Vue3和Element UI的el-upload二次封装,你可以参考以下步骤: 1. 首先,根据Element UI官方文档的指引,了解el-upload组件的使用方法和属性。你可以在官方文档中找到详细的代码示例和解释。 2. 创建一个Vue组件,例如UploadImg,用于封装el-upload组件。在组件中,你可以根据需要使用el-upload提供的上传、文件列表、删除等功能。你可以根据自己的需求,将el-upload的属性传递给子级组件或自定义函数进行处理。 3. 在组件中,你可以使用axios或其他HTTP库来进行文件的上传和删除操作。你可以根据自己的需要,封装适当的请求函数。比如,你可以创建一个delUpImage函数用于删除图片,或者创建一个uploadImg函数用于上传图片。 4. 在Vue的template中使用封装好的UploadImg组件,传递相应的属性和事件监听函数。这样,你就可以在页面中使用二次封装el-upload组件了。 下面是一个示例的代码结构: ```javascript // UploadImg.vue <template> <el-upload :limit="limit" action="" accept="image/*" :http-request="uploadFile" list-type="picture-card" :auto-upload="false" :file-list="fileList" :on-exceed="handleExceed" :before-remove="beforeRemove" ref="upload" > <img src="../assets/common_images/uploadImage.png" width="146" height="146"> </el-upload> <el-form-item> <el-button size="small" type="primary" style="margin-top: 20px;" @click="submitUpload">点击上传</el-button> </el-form-item> </template> <script> import { defineComponent } from 'vue'; import axios from 'axios'; export default defineComponent({ name: 'UploadImg', props: { limit: { type: Number, default: 6 }, fileList: { type: Array, default: () => [] } }, methods: { uploadFile(file) { // 上传文件的逻辑处理 }, handleExceed(files, fileList) { // 处理文件超出限制的逻辑 }, beforeRemove(file, fileList) { // 删除文件前的逻辑处理 }, submitUpload() { // 点击上传按钮的逻辑处理 } } }); </script> ``` 请注意,这只是一个示例代码,你需要根据自己的实际需求进行相应的修改和调整。另外,为了实现文件上传和删除操作,你可能还需要引入其他库或自定义函数来处理HTTP请求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值