vue使用input封裝上傳文件圖片全局組件的示例代碼
前言
- 實(shí)際開發(fā)過(guò)程中,我們經(jīng)常遇見需要上傳文件圖片功能,可以封裝一個(gè)全局組件來(lái)調(diào)用
- 原理很簡(jiǎn)單,首先獲取到文件或圖片對(duì)象,調(diào)用自己公司文檔服務(wù)器的接口,上傳文件圖片
- 為了方便用戶體驗(yàn),我們應(yīng)該在上傳之前開啟遮罩層,上傳成功之后關(guān)閉遮罩層。
- 我們還可以根據(jù)實(shí)際開發(fā)場(chǎng)景自定義把url和文件名稱傳回父組件
代碼實(shí)現(xiàn)
1.定義api
export function uploadvideo (data) { return request({ url: '/upload/video', method: 'post', headers: { 'Content-Type': 'multipart/form-data' }, data }) }
2.在src/components/建立DocUpload文件夾/index.vue-代碼如下
<template> <el-dialog title="上傳" :visible.sync="dialogVisible" width="40%" :before-close="handleClose" > <el-form ref="form" :model="form" size="small" label-width="80px"> <el-form-item label="文件名稱:"> <el-input v-model="form.contitle" disabled></el-input> </el-form-item> <el-form-item label="文件上傳:"> <div class="uppicture"> <input type="file" class="upinput" ref="file" @change="showimg" /> <i class="el-icon-plus" id="changes" @click="changeimg"></i> <p>上傳合同文件附件</p> </div> <el-button type="primary" class="uploadbutton" @click="addupload" >上傳附件</el-button > </el-form-item> </el-form> ? <span slot="footer" class="dialog-footer"> <el-button @click="handleClose" style="background: #f7f7f7" size="small" >取 消</el-button > <!-- <el-button type="primary" @click="upload">確 定</el-button> --> </span> </el-dialog> </template> ? <script> import { uploadvideo } from '@/api/entering' // 遮罩層 import { Loading } from 'element-ui' export default { name: 'DocUpload', data () { return { form: { // 合同名稱 contitle: '' }, formdata: {} } }, props: { // 顯示隱藏 dialogVisible: { type: Boolean, // 必傳 required: true } }, methods: { // 關(guān)閉之前 handleClose () { console.log('關(guān)閉之前') // .sync語(yǔ)法糖,單向數(shù)據(jù)流問(wèn)題, // 父組件傳遞給子組件的數(shù)據(jù),子組件直接修改會(huì)報(bào)錯(cuò),需要傳遞給父組件修改 this.$emit('update:dialogVisible', false) }, // 輸入款獲取事件 showimg () { const that = this console.log(that.$refs.file) console.log(that.$refs.file.files[0]) // 文件名稱復(fù)制 that.form.contitle = that.$refs.file.files[0].name // 聲明一個(gè)formdata對(duì)象 this.formdata = new FormData() // 賦值需要傳遞的文件 this.formdata.append('multipartFile', that.$refs.file.files[0]) }, // 圖標(biāo)觸發(fā)輸入框事件 changeimg () { // 點(diǎn)擊圖標(biāo)時(shí)候,觸發(fā)input選擇文件按鈕 this.$refs.file.dispatchEvent(new MouseEvent('click')) }, // 上傳附件 async addupload () { // 上傳文文件提示,未選擇文件提示用戶 if (!this.form.contitle) { return this.$message.warning('請(qǐng)先在左側(cè)上傳文件') } //開啟遮罩層 let loadingInstance = Loading.service({ lock: true, //lock的修改符--默認(rèn)是false text: '正在上傳文件,請(qǐng)耐心等待', //顯示在加載圖標(biāo)下方的加載文案 spinner: 'el-icon-loading', //自定義加載圖標(biāo)類名 background: 'rgba(0, 0, 0, 0.7)', //遮罩層顏色 target: document.querySelector('#futureTransferDialog') //loadin覆蓋的dom元素節(jié)點(diǎn) }) const res = await uploadvideo(this.formdata) console.log('文檔服務(wù)器上傳文件', res) // 傳遞文件存儲(chǔ)id this.$emit('updataurl', res.data.url) // 回顯文件名稱給父組件的form表單 this.$emit('updata', this.form.contitle) // 清空表單 this.form.contitle = '' this.formdata = {} // 關(guān)閉彈框 this.handleClose() //關(guān)閉遮罩層 loadingInstance.close() } } } </script> ? <style lang="scss" scoped> ::v-deep .el-dialog { border-radius: 10px; .el-dialog__header { border-radius: 9px 9px 0 0; background-color: #1488c6; padding: 8px 20px; .el-dialog__title { color: white; font-size: 16px; } ? .el-dialog__headerbtn { top: 12px; i { color: white; } } } .el-dialog__footer { text-align: center; } .el-dialog__body { padding: 12px; } .uppicture { width: 120px; height: 120px; border: 1px solid #717376; position: relative; cursor: pointer; input { width: 100%; height: 100%; vertical-align: middle; opacity: 0; } i { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 30px; // background-color: skyblue; } p { position: absolute; bottom: -2px; left: 50%; word-break: keep-all; transform: translate(-50%); } .uploadbutton { position: absolute; bottom: 0; margin-left: 20px; position: relative; } &:hover { color: #2da9fa; border: 1px solid #2da9fa; p { color: #2da9fa; } } } .uploadbutton { position: absolute; top: 70%; left: 150px; } } </style>
3.全局組件注冊(cè)-省略
4.父組件使用
4.1組件使用
<DocUpload :dialogVisible.sync="dialogannex" // form是父組件表單,上傳成功之后,直接通過(guò)子傳父,把url和文件名稱傳遞到父組件表單 @updata="form.name = $event" @updataurl="form.ontractAttachment = $event" ></DocUpload>
4.2父組件data
// 上傳組件開關(guān) dialogannex: false, // 表單 form: {},
4.3打開組件-methods
addupload () { this.dialogannex = true },
總結(jié):
經(jīng)過(guò)這一趟流程下來(lái)相信你也對(duì) vue-使用input封裝上傳文件圖片全局組件 有了初步的深刻印象,但在實(shí)際開發(fā)中我 們遇到的情況肯定是不一樣的,所以我們要理解它的原理,萬(wàn)變不離其宗。
到此這篇關(guān)于vue使用input封裝上傳文件圖片全局組件的文章就介紹到這了,更多相關(guān)vue封裝上下文件圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Vue的鉤子函數(shù)(路由導(dǎo)航守衛(wèi)、keep-alive、生命周期鉤子)
這篇文章主要介紹了詳解Vue的鉤子函數(shù)(路由導(dǎo)航守衛(wèi)、keep-alive、生命周期鉤子),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07vue項(xiàng)目中使用window的onresize事件方式
這篇文章主要介紹了vue項(xiàng)目中使用window的onresize事件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08詳解Vue iview IE瀏覽器不兼容報(bào)錯(cuò)(Iview Bable polyfill)
這篇文章主要介紹了Vue iview IE瀏覽器不兼容報(bào)錯(cuò)的決絕方法,由于Iview編譯使用到了es6的一些新特性,但是在IE中不支持ES6的新特性,本文就介紹一下如何解決這些問(wèn)題2019-01-01基于Vue實(shí)現(xiàn)自定義組件的方式引入圖標(biāo)
在vue項(xiàng)目中我們經(jīng)常遇到圖標(biāo),下面這篇文章主要給大家介紹了關(guān)于如何基于Vue實(shí)現(xiàn)自定義組件的方式引入圖標(biāo)的相關(guān)資料,文章通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07vue中provide?inject的響應(yīng)式監(jiān)聽解決方案
這篇文章主要介紹了vue中provide?inject的響應(yīng)式監(jiān)聽解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04Vue+Element UI+Lumen實(shí)現(xiàn)通用表格分頁(yè)功能
這篇文章主要介紹了Vue+Element UI+Lumen實(shí)現(xiàn)通用表格分頁(yè)功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02解決vux 中popup 組件Mask 遮罩在最上層的問(wèn)題
這篇文章主要介紹了解決vux 中popup 組件Mask 遮罩在最上層的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11VUE中this.$router.push點(diǎn)了后hash地址改變了頁(yè)面不跳轉(zhuǎn)
本文主要介紹了VUE中this.$router.push點(diǎn)了后hash地址改變了頁(yè)面不跳轉(zhuǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11