vue子組件改變父組件傳遞的prop值通過(guò)sync實(shí)現(xiàn)數(shù)據(jù)雙向綁定(DEMO)
最近開(kāi)始在用elementUI做一個(gè)后臺(tái)管理系統(tǒng)項(xiàng)目,遇到一個(gè)問(wèn)題,需求是這樣,在父組件有一個(gè)按鈕,點(diǎn)擊按鈕會(huì)顯示彈窗(子組件),在子組件中用的是elementUI 的el-diolog彈窗組件,在關(guān)閉彈窗時(shí)(elementUI自帶事件)便會(huì)報(bào)錯(cuò)。話不多說(shuō)直接上代碼。
DEMO
這是父組件的代碼:
<template>
<div>
<app-refund :dialogVisible="refundVisible"></app-refund>
// 下面是一個(gè)按鈕,點(diǎn)擊此按鈕會(huì)觸發(fā)refundFunc,并顯示彈窗
<el-dropdown-item @click.native="refundFunc">點(diǎn)擊此按鈕顯示彈窗</el-dropdown-item>
</div>
</template>
<script>
import refund from '@/pages/customer/refund'
export default {
components: {
"app-refund":refund
},
data(){
return {
refundVisible:false
}
},
methods: {
refundFunc:function(){
this.refundVisible=true
}
}
}
</script>
以下是子組件的代碼,為了使代碼看起來(lái)更方便簡(jiǎn)潔,已經(jīng)把其他冗余的代碼刪除,只留下能實(shí)現(xiàn)功能的必要代碼
<template>
<div>
<el-dialog
title="退余額"
:visible.sync="dialogVisible"
width="630px">
</el-dialog>
</div>
</template>
<script>
export default {
props:{
dialogVisible: {
type:Boolean,
default: false,
}
},
}
</script>
以上便是父組件控制子組件的顯示,而在子組件關(guān)閉彈窗的例子,這樣肯定是不行的,因?yàn)樵趘ue中props數(shù)據(jù)是單向流,不能在子組件改變父組件傳過(guò)來(lái)的prop值,而解決方式就是用emit來(lái)更新prop值,解決方案如下。
父組件代碼,js部分和上面一模一樣,這里就不重復(fù)寫了:
<template>
<div>
<app-refund :dialogVisible.sync="refundVisible"></app-refund>
// 下面是一個(gè)按鈕,點(diǎn)擊此按鈕會(huì)觸發(fā)refundFunc,并顯示彈窗
<el-dropdown-item @click.native="refundFunc">點(diǎn)擊此按鈕顯示彈窗</el-dropdown-item>
</div>
</template>
子組件代碼:
<template>
<div>
<el-dialog
title="退余額"
:visible.sync="dialogVisible"
:before-close="hidePanel"
width="630px">
</el-dialog>
</div>
</template>
<script>
export default {
props:{
dialogVisible: {
type:Boolean,
default: false,
}
},
methods: {
// 利用sync進(jìn)行數(shù)據(jù)雙向綁定,子組件修改dialogVisible的值,并響應(yīng)到父組件
hidePanel() {
this.$emit('update:dialogVisible', false)
}
},
}
</script>
這里用到了elementUI的before-close方法,是彈窗關(guān)閉前的回調(diào),用在這里的意思是在element自帶的關(guān)閉彈窗方法之前調(diào)用hidePanel方法,由我們來(lái)控制彈窗的關(guān)閉,這樣就能在關(guān)閉時(shí)更新dialogVisible的值,解決報(bào)錯(cuò)。
原理
很多時(shí)候我們需要在子組件中修改prop的值,這樣就破壞了vue的單項(xiàng)數(shù)據(jù)流,利用vue2.3的sync可以實(shí)現(xiàn)數(shù)據(jù)的雙向綁定,這是官方解釋 https://cn.vuejs.org/v2/guide... ,使用方式也很簡(jiǎn)單。
總結(jié)
以上所述是小編給大家介紹的vue子組件改變父組件傳遞的prop值通過(guò)sync實(shí)現(xiàn)數(shù)據(jù)雙向綁定,希望對(duì)大家有所幫助!
- Vue.js實(shí)現(xiàn)數(shù)據(jù)雙向綁定的代碼示例
- 使用Vue.js實(shí)現(xiàn)數(shù)據(jù)的雙向綁定
- Vue父子組件數(shù)據(jù)雙向綁定(父?jìng)髯?、子傳父)及ref、$refs、is、:is的使用與區(qū)別
- 淺析Vue3中通過(guò)v-model實(shí)現(xiàn)父子組件的雙向數(shù)據(jù)綁定及利用computed簡(jiǎn)化父子組件雙向綁定
- vue中如何解除數(shù)據(jù)之間的雙向綁定
- vue3中reactive數(shù)據(jù)被重新賦值后無(wú)法雙向綁定的解決
- proxy實(shí)現(xiàn)vue3數(shù)據(jù)雙向綁定原理
- vue自定義組件實(shí)現(xiàn)v-model雙向綁定數(shù)據(jù)的實(shí)例代碼
- vue 數(shù)據(jù)雙向綁定的實(shí)現(xiàn)方法
- Vue項(xiàng)目開(kāi)發(fā)實(shí)現(xiàn)父組件與子組件數(shù)據(jù)間的雙向綁定原理及適用場(chǎng)景
相關(guān)文章
Vue3實(shí)戰(zhàn)學(xué)習(xí)配置使用vue?router路由步驟示例
這篇文章主要為大家介紹了Vue3實(shí)戰(zhàn)學(xué)習(xí)配置使用vue?router路由步驟示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
對(duì)vuex中g(shù)etters計(jì)算過(guò)濾操作詳解
今天小編就為大家分享一篇對(duì)vuex中g(shù)etters計(jì)算過(guò)濾操作詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
vue3+vite使用element-plus問(wèn)題
這篇文章主要介紹了vue3+vite使用element-plus問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue3+axios+Mock.js實(shí)現(xiàn)登錄功能的示例代碼
本文主要介紹了Vue3+axios+Mock.js實(shí)現(xiàn)登錄功能的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05

