vue如何封裝選擇文件組件和選擇文件api
更新時(shí)間:2024年08月30日 17:21:25 作者:Mr阿斌
這篇文章主要介紹了vue如何封裝選擇文件組件和選擇文件api問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
vue封裝選擇文件組件和選擇文件api
方式一:選擇文件組件
<template> <div @click="clickHandle"> <slot></slot> <input type="file" hidden ref="inputRef" @change="changeFile" :accept="accept" :multiple="multiple" /> </div> </template>
<script> export default { name:'ChooseFile', props:{ accept:{ type:String }, multiple:{ type:Boolean, default:false } }, methods: { clickHandle() { this.$refs.inputRef.click() }, changeFile(e){ this.$emit('chooseFile',e.target.files) } }, } </script> <style scoped> </style>
方式二:選擇文件api
const ChooseFile = (options) => { if(typeof options ==='function'){ options={success:options} } if (typeof options === 'object') { let input = document.createElement("input") document.body.appendChild(input) input.type = 'file' input.hidden='hidden' if (options.accept) { input.accept = options.accept } if (options.multiple != null) { input.multiple = options.multiple } input.click() input.onchange = (e) => { options.success(e.target.files) document.body.removeChild(input) } } } export default ChooseFile
掛載在vue原型上
使用
this.$chooseFile((files)=>console.log(files))
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue全局自適應(yīng)大小:使用postcss-pxtorem方式
本文介紹了如何在Vue項(xiàng)目中使用postcss-pxtorem插件實(shí)現(xiàn)響應(yīng)式設(shè)計(jì),postcss-pxtorem可以自動(dòng)將CSS文件中的px單位轉(zhuǎn)換為rem單位,從而實(shí)現(xiàn)更好的自適應(yīng)布局,通過配置postcss-pxtorem插件,可以在構(gòu)建時(shí)自動(dòng)完成轉(zhuǎn)換,無需手動(dòng)修改代碼2025-01-01使用Webpack提升Vue.js應(yīng)用程序的4種方法(翻譯)
這篇文章主要介紹了使用Webpack提升Vue.js應(yīng)用程序的4種方法(翻譯),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10vue項(xiàng)目中使用qrcodesjs2生成二維碼簡(jiǎn)單示例
最近項(xiàng)目中需生成二維碼,發(fā)現(xiàn)了很好用的插件qrcodesjs2,所以下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目中使用qrcodesjs2生成二維碼的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05vue導(dǎo)出html、word和pdf的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue導(dǎo)出html、word和pdf的實(shí)現(xiàn)方法,文中完成了三種文件的導(dǎo)出但是還有很多種方法,小編就不給大家一一列舉了,需要的朋友可以參考下2018-07-07vue使用nprogress實(shí)現(xiàn)進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了vue使用nprogress實(shí)現(xiàn)進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12