關(guān)于Vue父子組件傳參和回調(diào)函數(shù)的使用
關(guān)鍵點(diǎn): 父組件給子組件動(dòng)態(tài)傳參使用v-bind:
屬性key(多個(gè)單詞用下劃線(xiàn)拼接) 子組件接收父組件傳參參數(shù)使用 props
標(biāo)簽,+屬性key多個(gè)單詞用駝峰形式拼接)
子組件定義回調(diào)父組件函數(shù) 子組件: v-on:change="uploadFile()
父組件: :after-upload=“afterUpload” 然后定一個(gè)afterUpload(resp)方法接收子組件傳過(guò)來(lái)的值
<div class="col-sm-10"> <file :text="'上傳頭像1'" :input-id="'image-upload'" :suffixs="[ ['jpg', 'jpeg', 'png']]" :after-upload="afterUpload"> </file> <script> import File from "../../components/file"; export default { components: {File}, name: "business-teacher", data: function () { }, mounted: function () { }, methods: { afterUpload(resp) { let _this = this let image = resp.content; _this.teacher.image = image } } } </script>
子組件
<template> <div> <button type="button" v-on:click="selectFile()" class="btn btn-white btn-default btn-round"> <i class="ace-icon fa fa-upload"></i> {{ text }} </button> <input class="hidden" type="file" ref="file" v-on:change="uploadFile()" v-bind:id="inputId+'-input'"> </div> </template> <script> export default { name: 'file', props: { text: { default: "上傳文件" }, inputId: { default: "file-upload" }, suffixs: { default: [] }, afterUpload: { type: Function, default: null }, }, data: function () { return {} }, methods: { uploadFile() { let _this = this; let formData = new window.FormData(); let file = _this.$refs.file.files[0]; // 判斷文件格式 let suffixs = _this.suffixs; let fileName = file.name; let suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length).toLowerCase(); let validateSuffix = false; for (let i = 0; i < suffixs.length; i++) { let suffix2 = suffixs[i] += '' if (suffix2.toLowerCase() === suffix) { validateSuffix = true; break; } } if (!validateSuffix) { Toast.warning("文件格式不正確,只支持上傳:" + suffixs.join(",")); //解決 同一個(gè)文件上傳2次或者大于2次,不會(huì)發(fā)生變化 $("#" + _this.inputId + "-input").val(""); return } // key:"file"必須和后端controller參數(shù)名一致 formData.append("file", file); Loading.show() _this.$api.post(process.env.VUE_APP_SERVER + '/file/admin/upload', formData).then((response) => { Loading.hide() let resp = response.data console.log("上傳文件成功:", resp) //回調(diào)父組件函數(shù) _this.afterUpload(resp) //解決 同一個(gè)文件上傳2次或者大于3次,不會(huì)發(fā)生變化 $("#" + _this.inputId + "-input").val(""); }) }, selectFile() { let _this = this // console.log("_this.inputId",_this.inputId) $("#" + _this.inputId + "-input").trigger("click"); } }, } </script> <style scoped> </style>
到此這篇關(guān)于關(guān)于Vue父子組件傳參和回調(diào)函數(shù)的使用的文章就介紹到這了,更多相關(guān)Vue父子組件回調(diào)函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 1.x 交互實(shí)現(xiàn)仿百度下拉列表示例
本篇文章主要介紹了vue 1.x 交互實(shí)現(xiàn)仿百度下拉列表示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10vue select組件的使用與禁用實(shí)現(xiàn)代碼
這篇文章主要介紹了vue--select組件的使用與禁用的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值 ,需要的朋友可以參考下2018-04-04詳解vue axios用post提交的數(shù)據(jù)格式
這篇文章主要介紹了詳解vue axios用post提交的數(shù)據(jù)格式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08el-tree樹(shù)設(shè)置懶加載以及設(shè)置默認(rèn)勾選方式
這篇文章主要介紹了el-tree樹(shù)設(shè)置懶加載以及設(shè)置默認(rèn)勾選方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04Vue實(shí)現(xiàn)封裝樹(shù)狀圖的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何使用Vue實(shí)現(xiàn)封裝樹(shù)狀圖,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03