Vue中子組件向父組件傳值以及.sync修飾符詳析
- 傳送門:Vue中 狀態(tài)管理器(vuex)詳解及應(yīng)用場(chǎng)景
- 傳送門:Vue中 $ attrs、$ listeners 詳解及使用
- 傳送門:Vue中 事件總線(eventBus)詳解及使用
- 傳送門:Vue中 provide、inject 詳解及使用
Vue中 常見的組件通信方式可分為三類
父子通信
父向子傳遞數(shù)據(jù)是通過 props,子向父是通過 events($emit);
通過父鏈 / 子鏈也可以通信($parent / $children);
ref 也可以訪問組件實(shí)例;
provide / inject;
$attrs/$listeners;
兄弟通信
Bus;
Vuex;
跨級(jí)通信
Bus;
Vuex;
provide / inject、
$attrs / $listeners、
在 Vue 中,子父組件最常用的通信方式就是通過 props 進(jìn)行數(shù)據(jù)傳遞,props 值只能在父組件中更新并傳遞給子組件。在子組件內(nèi)部,是不允許改變傳遞進(jìn)來的 props 值,這樣做是為了保證數(shù)據(jù)單向流通。但有時(shí)候,我們會(huì)遇到一些場(chǎng)景,需要在子組件內(nèi)部改變 props 屬性值并更新到父組件中,這時(shí)就需要用到 .sync 修飾符。
1. 之前的寫法
子組件中可以通過 $emit 向父組件通信,通過這種間接的方式改變父組件的 data,從而實(shí)現(xiàn)改變子組件 props 的值
父組件
<template>
<div>
<Child
:title="childTitle" @changeTitle="CTitle"
:subTitle="childSubTitle" @update:subTitle="val => childSubTitle = val">
</Child>
</div>
</template>
<script>
import Child from './child.vue'
export default {
data() {
return {
childTitle:'hello world',
childSubTitle:'你好',
}
},
components:{
Child
},
methods: {
CTitle(msg){
this.childTitle = msg;
},
},
}
</script>
子組件
<template>
<div class="child">
<h2>{{title}}</h2>
<h4>{{subTitle}}</h4>
<button @click="changeTitle">改變英文標(biāo)題</button>
<button @click="changeSubTitle">改變中文標(biāo)題</button>
</div>
</template>
<script>
export default {
data() {
return {
newTitle:'Vue',
newSubTitle:'明天也要努力'
};
},
props: {
title:{
type:String,
default:'',
},
subTitle:{
type:String,
default:'',
}
},
methods:{
changeTitle(){
this.$emit('changeTitle',this.newTitle)
},
changeSubTitle(){
this.$emit('update:subTitle',this.newSubTitle)
},
},
};
</script>
2. .sync 修飾符
為了方便這種寫法,vue 提供了.sync 修飾符,說白了就是一種簡(jiǎn)寫的方式,我們可以將其當(dāng)作是一種語(yǔ)法糖,比如 v-on: click 可以簡(jiǎn)寫為 @click。
而上邊父組件的這種寫法,換成 sync 的方式就像下邊這樣:
父組件
<template>
<div>
<h1>父組件:{{childSubTitle}}</h1>
<Child :subTitle.sync="childSubTitle"></Child>
</div>
</template>
<script>
import Child from './child.vue'
export default {
data() {
return {
childSubTitle:'你好',
}
},
components:{
Child
},
}
</script>
子組件
<template>
<div class="child">
<h4>{{subTitle}}</h4>
<button @click="changeSubTitle">改變中文標(biāo)題</button>
</div>
</template>
<script>
export default {
data() {
return {
newSubTitle:'明天也要努力'
};
},
props: {
subTitle:{
type:String,
default:'',
}
},
methods:{
changeSubTitle(){
this.$emit('update:subTitle',this.newSubTitle)
},
},
};
</script>
總結(jié):
父組件將 message 的值傳給子組件的,子組件中觸發(fā)事件 update:xxx 需要修改父組件的 message,就可以用 .sync 修飾符簡(jiǎn)化( sync 操作符的時(shí)候子組件 emit 必須是 ‘update:xxx’ 名字):
<child-cop :xxx.sync="message"></child-cop>
.sync 修飾符其實(shí)就是將上述代碼轉(zhuǎn)換成
<child-cop :xxx="message" @update:xxx="message = $event"></child-cop>
注意:

父組件
<template>
<div>
<h1>父組件:{{doc.title}}--{{doc.content}}</h1>
<Child v-bind.sync="doc"></Child>
</div>
</template>
<script>
import Child from './child.vue'
export default {
data() {
return {
doc:{
title:'前端',
content:'Vue',
},
}
},
components:{
Child
},
}
</script>
子組件
<template>
<div class="child">
<h4>{{title}}--{{content}}</h4>
<button @click="change">改變</button>
</div>
</template>
<script>
export default {
data() {
return {
newTitle:'明天也要努力'
};
},
props: {
title:{
type:String,
default:'',
},
content:{
type:String,
default:'',
}
},
methods:{
change(){
this.$emit('update:title',this.newTitle);
this.$emit('update:content','Vue中 子組件向父組件傳值 及 .sync 修飾符 詳解');
},
},
};
</script>
點(diǎn)擊按鈕后 效果

總結(jié)
到此這篇關(guān)于Vue中子組件向父組件傳值以及.sync修飾符的文章就介紹到這了,更多相關(guān)Vue子組件向父組件傳值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue.js多頁(yè)面開發(fā)環(huán)境搭建過程
利用 vue-cli 搭建的項(xiàng)目大都是單頁(yè)面應(yīng)用項(xiàng)目,對(duì)于簡(jiǎn)單的項(xiàng)目,單頁(yè)面就能滿足要求。這篇文章主要介紹了vue.js多頁(yè)面開發(fā)環(huán)境搭建 ,需要的朋友可以參考下2019-04-04
vue實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Grid拖拽布局
這篇文章主要為大家詳細(xì)介紹了如何利用vue實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Grid拖拽布局,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
vue實(shí)現(xiàn)倒計(jì)時(shí)獲取驗(yàn)證碼效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)倒計(jì)時(shí)獲取驗(yàn)證碼效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
VUE PC端可拖動(dòng)懸浮按鈕的實(shí)現(xiàn)代碼
這篇文章主要介紹了VUE PC端可拖動(dòng)懸浮按鈕的實(shí)現(xiàn)代碼,通過實(shí)例代碼介紹了父頁(yè)面引用的方法,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02
Vue實(shí)現(xiàn)用戶自定義字段顯示數(shù)據(jù)的方法
今天小編就為大家分享一篇Vue實(shí)現(xiàn)用戶自定義字段顯示數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Vue實(shí)現(xiàn)返回頂部按鈕實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)返回頂部按鈕的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
詳解使用jest對(duì)vue項(xiàng)目進(jìn)行單元測(cè)試
這篇文章主要介紹了詳解使用jest對(duì)vue項(xiàng)目進(jìn)行單元測(cè)試,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09

