二次封装el-update组件(vue2.x)

Vue2.x二次封装el-upload组件
这篇博客主要介绍了在Vue2.x中如何对element-ui的el-upload组件进行二次封装,详细展示了单张图片上传的实现过程,包括Upload.vue组件的编写以及在项目中的引入和使用方法。多张图片上传的功能目前处于待更新状态。

一、单张图片上传

组件 Upload.vue

<template>
  <div>
    <el-upload
      class="pic-uploader-component"
      action="上传地址"
      :show-file-list="false"
      :on-success="handleUploadSuccess"
      :on-change="handleChange"
      :before-upload="beforeAvatarUpload"
    >
      <img v-if="value && !loading" :src="resourcesUrl + value" class="pic">
      <i v-if="!value && !loading" class="el-icon-plus pic-uploader-icon" />
      <i v-if="loading" class="el-icon-loading pic-uploader-icon" />
    </el-upload>
  </div>
</template>
<script>
export default {
  props: {
    value: {
      default: '',
      type: String
    }
  },
  data() {
    return {
        loading: false,
        resourcesUrl: '',
    }
  },
  methods: {
    handleChange(){
        this.loading = false
    },
    // 图片上传
    handleUploadSuccess(response, file, fileList) {
        console.log(file)
        this.$emit('input', file.response)
        this.loading = false
    },
    // 限制图片上传大小
    beforeAvatarUpload(file) {
        this.loading = true
        const isLt2M = file.size / 1024 / 1024 < 2
        if (!isLt2M) {
            this.$message.error('上传图片大小不能超过 2MB!')
        }
        return isLt2M
    }
  }
}
</script>
<style lang="scss">
  .pic-uploader-component .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    .pic-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 120px;
      height: 120px;
      line-height: 120px;
      text-align: center;
    }
    .pic {
      width: 120px;
      height: 120px;
      display: block;
    }
  }
  .pic-uploader-component .el-upload:hover {
    border-color: #409EFF;
  }
</style>

引入注册后的使用方式

<upload v-model="url"></upload>

二、多张图片上传(待更新)

### 封装 Vue3 Element-Plus `el-cascader` 组件Vue3 中对 Element-Plus 的 `el-cascader` 进行二次封装,可以通过抽象常用属性和方法来简化其使用流程。以下是实现这一目标的具体方式: #### 1. 创建基础封装组件 创建一个新的文件 `TCascader.vue` 来定义封装后的 Cascader 组件。 ```vue <template> <el-cascader v-model="currentValue" :options="cascaderOptions" :props="cascaderProps" @change="handleCascaderChange" clearable /> </template> <script> import { defineComponent, ref, watch } from 'vue'; export default defineComponent({ name: 'TCascader', props: { modelValue: { type: Array, required: true, }, options: { type: Array, required: true, }, customProps: { type: Object, default: () => ({}), }, }, emits: ['update:modelValue', 'change'], setup(props, { emit }) { const currentValue = ref([...props.modelValue]); const cascaderOptions = ref(props.options); const cascaderProps = ref({ ...props.customProps }); // 监听外部传入的modelValue变化 watch( () => props.modelValue, (newValue) => { currentValue.value = [...newValue]; } ); // 处理级联选择器的变化事件 const handleCascaderChange = (value) => { emit('update:modelValue', value); // 更新父组件中的绑定值 emit('change', value); // 发送 change 事件给父组件 }; return { currentValue, cascaderOptions, cascaderProps, handleCascaderChange, }; }, }); </script> ``` 上述代码实现了以下功能: - 使用 `v-model` 实现双向数据绑定[^1]。 - 提供自定义选项配置通过 `customProps` 属性传递[^2]。 - 支持监听并触发 `change` 事件以便于开发者处理逻辑。 #### 2. 在项目中引入并使用该组件 下面是一个简单的例子展示如何在页面上使用这个封装好的 `TCascader` 组件。 ```vue <template> <div> <t-cascader v-model="selectedValues" :options="regionOptions" :custom-props="{ checkStrictly: true }" @change="onRegionChange" /> </div> </template> <script> import TCascader from './components/TCascader.vue'; import { defineComponent, reactive, toRefs } from 'vue'; export default defineComponent({ components: { TCascader, }, setup() { const state = reactive({ selectedValues: [], regionOptions: [ { value: 'zhinan', label: '指南', children: [ { value: 'shejiyuanze', label: '设计原则', children: [{ value: 'yizhi', label: '一致' }], }, ], }, ], }); const onRegionChange = (values) => { console.log('Selected Region:', values); }; return { ...toRefs(state), onRegionChange, }; }, }); </script> ``` 这段代码展示了如何利用封装过的 `TCascader` 并设置初始参数以及响应改变事件。 --- ####
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值