Vue中sync修飾符分析原理及用法示例
前幾天在看別人代碼時(shí),發(fā)現(xiàn)了sync修飾符的妙用,特記錄其用法和原理如下。
不使用sync修飾符的代碼示例
父組件:
<template>
<div>
<div v-if="show">11111</div>
<h3>下面是子組件</h3>
<SyncDemo :show="show" @update="update"></SyncDemo>
</div>
</template>
<script>
import SyncDemo from "./SyncDemo.vue";
export default {
name: "Test",
components: { SyncDemo },
props: [],
data() {
return {
show: true,
};
},
methods: {
update(flag) {
this.show = flag;
},
},
};
</script>子組件:
<template>
<div>
<button @click="changeFlag">點(diǎn)擊</button>
</div>
</template>
<script>
export default {
name: "SyncDemo",
components: {},
props: ["show"],
methods: {
changeFlag() {
this.$emit("update", !this.show);
},
},
};
</script>
點(diǎn)擊子組件的按鈕就可以控制父組件“11111”的顯示與隱藏。
使用sync修飾符,代碼就會簡單很多。
使用sync修飾符的代碼示例
父組件:
<template>
<div>
<div v-if="show">11111</div>
<h3>下面是子組件</h3>
<SyncDemo :show.sync="show"></SyncDemo>
</div>
</template>
<script>
import SyncDemo from "./SyncDemo.vue";
export default {
name: "Test",
components: { SyncDemo },
props: [],
data() {
return {
show: true,
};
},
};
</script>子組件:
<template>
<div>
<button @click="changeFlag">點(diǎn)擊</button>
</div>
</template>
<script>
export default {
name: "SyncDemo",
components: {},
props: ["show"],
methods: {
changeFlag() {
this.$emit("update:show", !this.show);
},
},
};
</script>可以看到使用sync修飾符之后,父組件就不需要向子組件傳遞方法,父組件也不需要特意寫修改變量的方法了。此時(shí)點(diǎn)擊子組件的按鈕,一樣可以控制父組件的“11111”的顯示與隱藏。
sync修飾符的原理
在不使用sync時(shí)父組件中子組件綁定的update事件沒有傳遞參數(shù),其實(shí)綁定事件默認(rèn)傳遞了$event參數(shù)。

這里值得注意的是如果綁定的是clikc等原生事件時(shí),$event是指事件對象,如果是自定義事件,$event則指子組件觸發(fā)該方法時(shí)傳遞的參數(shù),在上面的例子中,$event則指show變量。
所以不使用sync修飾符時(shí),父組件的代碼可以改造成如下(直接將$event賦值給show變量,父組件不需要再另外寫一個(gè)修改變量的方法):
<template>
<div>
<div v-if="show">11111</div>
<h3>下面是子組件</h3>
<SyncDemo :show="show" @update="show = $event"></SyncDemo>
</div>
</template>
<script>
import SyncDemo from "./SyncDemo.vue";
export default {
name: "Test",
components: { SyncDemo },
props: [],
data() {
return {
show: true,
};
},
};
</script>我們再進(jìn)一步修改代碼(綁定方法時(shí)指明要修改的變量):


<SyncDemo :show="show" @update:show="show = $event"></SyncDemo>
這行代碼的語法糖的方式就是使用sync修飾符(代碼變得更簡單)
<SyncDemo :show.sync="show"></SyncDemo>
以上就是通過一步步代碼演變,最后變成使用sync修飾符的方式修改父組件的變量,使我們明白了sync修飾符的原理。
現(xiàn)粘貼vue官網(wǎng)上關(guān)于sync修飾符的解釋:

到此這篇關(guān)于Vue中sync修飾符分析原理及用法示例的文章就介紹到這了,更多相關(guān)Vue sync修飾符內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue基于iview實(shí)現(xiàn)登錄密碼的顯示與隱藏功能
這篇文章主要介紹了Vue基于iview實(shí)現(xiàn)登錄密碼的顯示與隱藏功能,本文通過截圖實(shí)例代碼說明給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Vue進(jìn)階之CodeMirror的應(yīng)用小結(jié)
CodeMirror支持在線編輯代碼,風(fēng)格包括js, java, php, c++等等100多種語言,下面這篇文章主要來和大家講講CodeMirror的應(yīng)用,感興趣的可以了解一下2023-06-06
解決vue項(xiàng)目 build之后資源文件找不到的問題
這篇文章主要介紹了解決vue項(xiàng)目 build之后資源文件找不到的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
一文帶你了解threejs在vue項(xiàng)目中的基本使用
three.js是一個(gè)用于在Web上創(chuàng)建三維圖形的JavaScript庫,它可以用于創(chuàng)建各種類型的三維場景,包括游戲、虛擬現(xiàn)實(shí)、建筑和產(chǎn)品可視化等,下面這篇文章主要給大家介紹了關(guān)于如何通過一文帶你了解threejs在vue項(xiàng)目中的基本使用,需要的朋友可以參考下2023-04-04
vue中異步數(shù)據(jù)獲取方式(確保數(shù)據(jù)被獲取)
這篇文章主要介紹了vue中異步數(shù)據(jù)獲取方式(確保數(shù)據(jù)被獲取),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

