關(guān)于Vue父子組件傳參和回調(diào)函數(shù)的使用
關(guān)鍵點(diǎn): 父組件給子組件動(dòng)態(tài)傳參使用v-bind:屬性key(多個(gè)單詞用下劃線拼接) 子組件接收父組件傳參參數(shù)使用 props標(biāo)簽,+屬性key多個(gè)單詞用駝峰形式拼接)
子組件定義回調(diào)父組件函數(shù) 子組件: v-on:change="uploadFile()
父組件: :after-upload=“afterUpload” 然后定一個(gè)afterUpload(resp)方法接收子組件傳過來的值
<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腳手架工具搭建vue-webpack項(xiàng)目
本篇文章主要介紹了詳解使用vue腳手架工具搭建vue-webpack項(xiàng)目,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05
使用vuex較為優(yōu)雅的實(shí)現(xiàn)一個(gè)購物車功能的示例代碼
這篇文章主要介紹了使用vuex較為優(yōu)雅的實(shí)現(xiàn)一個(gè)購物車功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Vue v-model相關(guān)知識(shí)總結(jié)
這篇文章主要介紹了Vue ​v-model相關(guān)知識(shí)總結(jié),幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-01-01
Vue引入Stylus知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給各位整理的是一篇關(guān)于Vue引入Stylus知識(shí)點(diǎn)總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。2020-01-01
uniapp 小程序和app map地圖上顯示多個(gè)酷炫動(dòng)態(tài)的標(biāo)點(diǎn)效果(頭像后端傳過來)
這篇文章主要介紹了uniapp 小程序和app map地圖上顯示多個(gè)酷炫動(dòng)態(tài)的標(biāo)點(diǎn)效果(頭像后端傳過來),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
vue.js模仿京東省市區(qū)三級(jí)聯(lián)動(dòng)的選擇組件實(shí)例代碼
選擇省市區(qū)是我們大家在填寫地址的時(shí)候經(jīng)常會(huì)遇到的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于利用vue.js模仿實(shí)現(xiàn)京東省市區(qū)三級(jí)聯(lián)動(dòng)選擇組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-11-11
vue-router中的hash和history兩種模式的區(qū)別
大家都知道vue-router有兩種模式,hash模式和history模式,這里來談?wù)剉ue-router中的hash和history兩種模式的區(qū)別。感興趣的朋友一起看看吧2018-07-07

