Vue中父子組件如何實(shí)現(xiàn)傳值
前言
提示:這里可以添加本文要記錄的大概內(nèi)容
項(xiàng)目中往往會把一些常用的公共代碼抽離出來,寫成一個(gè)子組件?;蛘咴谝粋€(gè)頁面中的代碼太多,可以根據(jù)功能的不同抽離出相關(guān)代碼寫成子組件,這樣代碼結(jié)構(gòu)會更加簡潔明了,后續(xù)維護(hù)更加方便。
本位將以一個(gè)項(xiàng)目中用到的例子進(jìn)行描述哈
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、將子組件引入父組件
父子組件頁面展示情況:
父組件引入子組件相關(guān)代碼:
示例:pandas 是基于NumPy 的一種工具,該工具是為了解決數(shù)據(jù)分析任務(wù)而創(chuàng)建的。
二、父組件如何傳值給子組件
父組件:通過數(shù)據(jù)綁定將數(shù)據(jù)傳遞給子組件
//父組件 <device-dialog :personInfo="info" //父組件通過賦值給info將表單數(shù)據(jù)傳給子組件 :action="action" //判斷是新增還是編輯 add、edit :dialogText="dialogText" //對話框顯示的標(biāo)題 @toClose="dialogConfirm('cancel')" //關(guān)閉按鈕對應(yīng)的函數(shù) @toComfirm="requestApi('getUser')" //確定按鈕對應(yīng)的函數(shù) v-if="this.dialogFormNew" //控制對話框的顯示與隱藏 ></device-dialog> this.info = this.form_user; //父組件中相關(guān)的值賦值給info this.dialogText = '編輯設(shè)備信息'; this.dialogFormNew = true;
三、子組件如何接收父組件傳過來的值并使用(props)
1. 通過props接收父組件的傳值
<script> export default { name: 'personDetail', props: { personInfo: { type: Object, default: {} }, action: { type: String, default: {} }, dialogText: { type: String, default: {} } } } </script>
2. 將props的值賦值給子組件里的數(shù)組容器從而展示父組件傳過來的數(shù)據(jù)
created() { this.form_user = this.personInfo; //form_user是子組件對話框顯示表單數(shù)據(jù)的數(shù)組 }
四、子組件如何傳值給父組件($emit)
子組件:這里主要通過子組件對話框取消和確定兩個(gè)點(diǎn)擊事件進(jìn)行傳值
</template> </div> <el-dialog> <div slot="footer" class="dialog-footer" > <el-button @click="dialogConfirm('cancel')">取 消</el-button> <el-button type="primary" @click="dialogConfirm('confirm')" > 確 定 </el-button> </div> </el-dialog> </div> </template> <script> dialogConfirm(res) { if (this.action === 'edit') { if (res === 'cancel') { this.closeDialog(); } else { this.$refs['userForm'].validate((valid) => { //驗(yàn)證成功 if (valid) { this.requestApi('edit'); //進(jìn)入api新增請求 } else { //驗(yàn)證失敗 console.log('error submit!!'); return false; } }); } } }, closeDialog() { this.$emit('toClose', false); //通過$emit將參數(shù)false傳值給父組件toClose對應(yīng)的函數(shù) }, requestApi(action, verifyCB) { switch (action) { // 查看編輯 case 'edit': this.$axios({ method: 'post', url: '/office/roomDevice/updateLock?random=' + Math.random() * 10, data: { id: this.form_user.deviceId, deviceName: this.form_user.deviceName, deviceAddress: this.form_user.deviceAddress }, headers: { Authorization: 'Bearer ' + sessionStorage.getItem('token') } }) .then((res) => { console.log('編輯_res:', res); if (res.data.code === 0) { this.tips(res.data.message, 'success'); this.comfirm(); } else { this.tips(res.data.message, 'warning'); } }) .catch((error) => { console.error(error); }); break; }, comfirm() { this.$emit('toComfirm'); //這里將相關(guān)的數(shù)據(jù)傳回父組件 } </script>
五、父組件使用子組件傳過來的值
如下面的代碼所示,closeData和confirmData分別接收取消和確定時(shí)子組件傳過來的值
總結(jié)
1、父子組件之間的傳參用的比較多的就是當(dāng)父組件向子組件傳遞參數(shù)時(shí),子組件用props接收參數(shù),父組件綁定要傳的參數(shù)。
2、子組件傳遞參數(shù)給父組件時(shí)試用$emit(父組件函數(shù),要傳的參數(shù))來傳遞數(shù)向父組件傳遞參數(shù)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue的rules驗(yàn)證部分可以部分又失效的原因及解決方案
vue的rules驗(yàn)證失效,部分可以部分又失效,很多百度都有,但是我這里遇到了一個(gè)特別的,那就是prop沒有寫全,導(dǎo)致驗(yàn)證某一個(gè)失效,接下來就跟小編一起來看看這個(gè)失效的原因和解決方案吧2023-11-11vue.js默認(rèn)路由不加載linkActiveClass問題的解決方法
這篇文章主要給大家介紹了關(guān)于vue.js默認(rèn)路由不加載linkActiveClass問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-12-12Vue如何基于vue-i18n實(shí)現(xiàn)多國語言兼容
這篇文章主要介紹了Vue如何基于vue-i18n實(shí)現(xiàn)多國語言兼容,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07elementUI el-input 只能輸入正整數(shù)驗(yàn)證的操作方法
這篇文章主要介紹了elementUI el-input 只能輸入正整數(shù)驗(yàn)證,本文給大家詳細(xì)講解對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11