封装elementUI-按钮组组件

<template>
  <div class="button-group">
    <ul v-for="(item,index) in button_group_list" :key="index">
      <li v-for='(button,i) in item' :key="i">
        <!-- 文件上传 -->
        <el-upload v-if='button.action'
          :disabled="button.disabled?button.disabled:false"
          :accept="button.accept?button.accept:'.xls,.xlsx'" 
          :action="button.action?button.action:''"
          :data="button.data?button.data:null" 
          :multiple='button.multiple?button.multiple:false'
          :show-file-list="button.showFileList?button.showFileList:false"
          :on-success="uploadSuccess"
          :on-error="uploadError">
          <el-button 
            size="mini" 
            type="text" 
            :disabled="button.disabled?button.disabled:false"
            @click="buttonClick(button.event)">
              <icon class="icon" :name="button.svg" scale="2.625"></icon>
              <p>{{button.text}}</p>
          </el-button>
        </el-upload>
        <!-- 默认的button -->
        <el-button v-else 
          size="mini" 
          type="text" 
          :disabled="button.disabled?button.disabled:false" 
          @click="buttonClick(button.event)">
            <icon class="icon" :name="button.svg" scale="2.625"></icon>
            <p>{{button.text}}</p>
        </el-button>
      </li>
    </ul>
  </div>
</template>

<script>
  export default {
    name: 'buttonGroup',
    data () {
      return {
      }
    },
    props:{
      button_group_list:{
        type:Array,
        require:true,
        defalut(){
          return []//数组形式
          /* 父组件中的参数格式:
            buttonGroupList:[
              [//每个数组就是一个ul
                {
                  svg:'chaxun',//svg的名字,必传
                  text:'查询',//按钮的名称,必传
                  event:'query'//点击按钮触发的事件名,必传
                },
                {
                  svg:'xinzeng',
                  text:'新增',
                  event:'add'
                },
                {
                  svg:'xiugai',
                  text:'修改',
                  event:'update'
                },
                {
                  svg:'shanchu',
                  text:'删除',
                  event:'del'
                },
              ],
              [
                {
                  svg:'daoru',
                  text:'导入',
                  action:setIP() + "ztgg/zqrl/import",//必传,根据它来判断是否启用upload组件
                  data:{nd:''},//要在其他地方给复制,这里赋不了值
                  multiple:false,//非必传,默认就是false
                  showFileList:false,//非必传,默认就是false
                  // event:'daoru'
                },
                {
                  svg:'daochu',
                  text:'导出',
                  event:'export'
                },
              ]
            ],
          */
        }
        
      },
    },
    methods:{
     
      /* 按钮的点击事件 */
      buttonClick(eventName){
        console.log(eventName)
        if(eventName){
          this.$emit(eventName)//触发父组件的点击事件名@eventName 对应的button传过来什么名字父组件就@什么名字
        }
      },

      /* 若有el-upload,则父组件就会有上传成功的钩子 */
      uploadSuccess(response, file, fileList){
        console.log(response, file, fileList)
        this.$emit('uploadSuccess',response, file, fileList)//触发父组件上传成功的回调@uploadSuccess
      },

      /* 若有el-upload,则父组件就会有上传失败的钩子 */
      uploadError(err, file, fileList){
        console.log(err, file, fileList)
        this.$emit('uploadError',err, file, fileList)触发父组件上传成功的回调@uploadError
      },
    },
    created(){
      console.log(this.button_group_list)
    }
  }
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
.button-group svg{
  width: 20px;
  height: 20px;
}
.button-group{
  height: 46px !important;
}
.button-group ul{
  padding: 0 ;
  list-style: none;
  display: inline-block;
  margin:0 10px 4px 0 ;
}
.button-group ul:last-child{
  border-right: none;
}
.button-group .el-button{
  padding: 0 !important;
}
.button-group ul li{
  float: left;
}
.button-group ul li:last-child{
 padding-right: 16px;
}
.button-group .el-button p{
  margin: 8px;
  line-height: 0;
  font-size: 12px;
  -webkit-transform: scale(0.9);
  color: rgba(0,0,0,0.65);
}
.button-group .svg-icon:hover{
  opacity: 0.8;
}

</style>

效果:
在这里插入图片描述

封装 Element UI 的上传组件,你可以创建一个自定义的 Vue 组件,然后在其中使用 Element UI 的 el-upload 组件。以下是一个简单的封装示例: ```vue <template> <div> <el-upload ref="upload" :action="uploadUrl" :on-success="handleSuccess" :before-upload="beforeUpload" :auto-upload="autoUpload" > <slot></slot> </el-upload> </div> </template> <script> export default { name: 'CustomUpload', props: { uploadUrl: { type: String, required: true }, autoUpload: { type: Boolean, default: true } }, methods: { handleSuccess(response, file, fileList) { // 处理上传成功的回调逻辑 this.$emit('upload-success', response, file, fileList); }, beforeUpload(file) { // 可以在这里添加一些上传前的逻辑判断,如文件类型、大小等 return true; // 返回 true 表示允许上传 }, clearFiles() { this.$refs.upload.clearFiles(); }, abortUpload() { this.$refs.upload.abort(); } } }; </script> ``` 在这个示例中,我们创建了一个 `CustomUpload` 组件,它接受一个 `uploadUrl` 属性来指定上传地址,并且还有一个可选的 `autoUpload` 属性来控制是否自动上传。 你可以在组件中添加自定义的逻辑和样式,并使用 `this.$refs.upload` 来访问 Element UI 的上传组件实例,从而调用其方法和获取上传的文件信息。 使用示例: ```vue <template> <div> <custom-upload upload-url="上传地址" :auto-upload="false" @upload-success="handleUploadSuccess" > <el-button size="small" type="primary">点击上传</el-button> </custom-upload> </div> </template> <script> import CustomUpload from '@/components/CustomUpload.vue'; export default { components: { CustomUpload }, methods: { handleUploadSuccess(response, file, fileList) { console.log('上传成功'); } } }; </script> ``` 在上面的示例中,我们在 `CustomUpload` 组件中使用了一个插槽,用来插入自定义的上传按钮。当文件上传成功后,会触发 `upload-success` 事件,你可以在父组件中监听该事件并处理相应的逻辑。 这样,你就可以通过封装 Element UI 的上传组件来方便地在项目中使用了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值