编程随笔 编程随笔
  • 前端
  • 后端
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)

liyao52033

走运时,要想到倒霉,不要得意得过了头;倒霉时,要想到走运,不必垂头丧气。心态始终保持平衡,情绪始终保持稳定,此亦长寿之道
  • 前端
  • 后端
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)
  • 知识点

  • 代码调试

  • vue2

    • vue-element-admin使用
    • el-upload封装
    • 文件上传
    • mavon-editor局部引入
    • vuex使用
  • vue3

  • react

  • typescript

  • 前端
  • vue2
华总
2023-08-25
0
0

el-upload封装原创

父组件模板

<template>
  <div>
    <upload-component @file-selected="handleFileSelected"></upload-component>
  </div>
</template>

<script>
import UploadComponent from './UploadComponent.vue';

export default {
  components: {
    UploadComponent
  },
  methods: {
    handleFileSelected(file) {
      // 处理选中的文件
      console.log('选中的文件:', file);
      // 执行其他逻辑
    }
  }
};
</script>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

子组件模板(UploadComponent.vue)

<template>
  <div>
    <el-upload
      class="upload-demo"
      :action="uploadUrl"
      :on-change="handleFileChange"
    >
      <el-button slot="trigger">选取文件</el-button>
      <el-button style="margin-left: 10px;">上传到服务器</el-button>
    </el-upload>
  </div>
</template>

<script>
export default {
  methods: {
    handleFileChange(file) {
      // 触发自定义事件将文件传递给父组件
      this.$emit('file-selected', file);
    }
  }
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

在子组件中,当文件发生变化时,通过this.$emit方法触发file-selected事件,并将文件作为参数传递给父组件。

在父组件中,使用@file-selected监听子组件触发的file-selected事件,并在handleFileSelected方法中处理选中的文件。

这样,当在子组件中选择文件时,会将文件传递给父组件,并在父组件中处理选中文件的逻辑。

请根据你的项目需求进行相应的修改。

#前端
上次更新: 2024/01/14 16:27:31
vue-element-admin使用
文件上传

← vue-element-admin使用 文件上传→

最近更新
01
element-plus多文件手动上传 原创
11-03
02
TrueLicense 创建及安装证书 原创
10-25
03
手动修改迅捷配置 原创
09-03
04
安装 acme.sh 原创
08-29
05
zabbix部署 原创
08-20
更多文章>
Copyright © 2023-2024 liyao52033
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式