最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue .sync修飾符的使用詳解

 更新時間:2018年06月15日 13:45:39   作者:再見眸似霜  
這篇文章主要介紹了vue .sync修飾符的使用,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下

vue的官網(wǎng)介紹非常不錯,先通讀一遍。

2.3.0+ 新增

在有些情況下,我們可能需要對一個 prop 進行“雙向綁定”。不幸的是,真正的雙向綁定會帶來維護上的問題,因為子組件可以修改父組件,且在父組件和子組件都沒有明顯的改動來源。

這也是為什么我們推薦以 update:my-prop-name 的模式觸發(fā)事件取而代之。舉個例子,在一個包含  title prop 的假設(shè)的組件中,我們可以用以下方法表達對其賦新值的意圖:

this.$emit('update:title', newTitle)

然后父組件可以監(jiān)聽那個事件并根據(jù)需要更新一個本地的數(shù)據(jù)屬性。例如:

<text-document
 v-bind:title="doc.title"
 v-on:update:title="doc.title = $event"
></text-document>

為了方便起見,我們?yōu)檫@種模式提供一個縮寫,即 .sync 修飾符:

<text-document v-bind:title.sync="doc.title"></text-document>

當我們用一個對象同時設(shè)置多個 prop 的時候,也可以將這個 .sync 修飾符和  v-bind 配合使用:

<text-document v-bind.sync="doc"></text-document>

這樣會把 doc 對象中的每一個屬性 (如  title ) 都作為一個獨立的 prop 傳進去,然后各自添加用于更新的  v-on 監(jiān)聽器。

以下為代碼實例

father.vue

<template>
 <div class="hello">
 //input實時改變wrd的值, 并且會實時改變box里的內(nèi)容
 <input type="text" v-model="wrd">
 <box :word.sync="wrd" ></box>
 </div>
</template>
<script>
import box from './box' //引入box子組件
export default {
 name: 'HelloWorld',
 data() {
 return {
  wrd: ''
 }
 },
 components: {
 box
 } 
}
</script>
child.vue
<template>
 <div class="hello">
 <div class="ipt">
  <input type="text" v-model="str">
 </div>
 //word是父元素傳過來的
 <h2>{{ word }}</h2>
 </div>
</template>
<script>
export default {
 name: 'box',
 data() {
 return {
  str: '',
 }
 },
 props: {
 word: ''
 },
 watch: {
 str: function(newValue, oldValue) {
  //每當str的值改變則發(fā)送事件update:word , 并且把值傳過去
  this.$emit('update:word', newValue)
 }
 }
}
</script>

那這個修飾符的原理是什么呢?其實還是vue父組件可以監(jiān)聽子組件的事件,自組件可以觸發(fā)父組件的事件

father.vue

<template>
 <div class="hello">
 <input type="text" v-model="wrd">
 <box @incre="boxIncremend" ></box>
 </div>
</template>
<script>
import box from './box'
export default {
 name: 'HelloWorld',
 data() {
 return {
  wrd: ''
 }
 },
 methods: {
 boxIncremend(e) {
  this.wrd = this.wrd + e
 }
 },
 components: {
 box
 }
}
</script>
child.vue
<template>
 <div class="hello">
  <input type="text" v-model="str">
 <h2>{{ word }}</h2>
 </div>
</template>
<script>
export default {
 name: 'box',
 data() {
 return {
  num: 0
 }
 },
 props: {
 word: ''
 },
 watch: {
 str: function(neww, old) {
  //往父級發(fā)射incre事件
  this.$emit('incre', ++this.num)
 }
 },
}
</script>

在沒有sync的時候,我們要實現(xiàn)雙向綁定,可能需要在父組件里綁定一個事件和一個值

father.vue

<template>
 <div class="hello">
 <input type="text" v-model="wrd">
 <box @incre="boxIncremend" :word="word"></box>
 </div>
</template>
<script>
import box from './box'
export default {
 name: 'HelloWorld',
 data() {
 return {
  word: ''
 }
 },
 methods: {
 boxIncremend(newword) {
  this.word = newword
 }
 },
 components: {
 box
 }
}
</script>
child.vue
<template>
 <div class="hello">
  <input type="text" v-model="str">
 <h2>{{ word }}</h2>
 </div>
</template>
<script>
export default {
 name: 'box',
 props: {
 word: ''
 },
 watch: {
 str: function(newword) {
  //往父級發(fā)射incre事件
  this.$emit('incre', newword)
 }
 },
}
</script>

但是有了sync之后,我們就可以這么寫

father.vue

<template>
 <div class="hello">
 <input type="text" v-model="wrd">
 <box :wrd.sync="wrd"></box>
 </div>
</template>
<script>
import box from './box'
export default {
 name: 'HelloWorld',
 data() {
 return {
  wrd: ''
 }
 },
 methods: {
 boxIncremend(e) {
  this.wrd = e
 }
 },
 components: {
 box
 }
}
</script>

child.vue

<template>
 <div class="hello">
  <input type="text" v-model="str">
 <h2>{{ word }}</h2>
 </div>
</template>
<script>
export default {
 name: 'box',
 props: {
 word: ''
 },
 watch: {
 str: function(newword) {
  //往父級發(fā)射incre事件
  this.$emit('update:word', newword)
 }
 },
}
</script>

父組件中的子組件,少寫了一個自定義事件屬性,子組件中$emit直接出發(fā)父組件中數(shù)據(jù)的更新,清新明了。使用中需要注意的是,update和后面對應的數(shù)據(jù)名不能寫錯。

父子組件的雙向數(shù)據(jù)綁定

父組件改變數(shù)據(jù)可以改變子組件, 但是子組件的內(nèi)容改變并不會影響到父組件

可以通過2.3.0新增的sync修飾符來達到雙向綁定的效果

father.vue

<template>
 <div class="hello">
 //input實時改變wrd的值, 并且會實時改變box里的內(nèi)容
 <input type="text" v-model="wrd">
 <box :word.sync="wrd" ></box>
 </div>
</template>
<script>
import box from './box' //引入box子組件
export default {
 name: 'HelloWorld',
 data() {
 return {
  wrd: ''
 }
 },
 components: {
 box
 } 
}
</script>
<style scoped></style>

box.vue

<template>
 <div class="hello">
 <div class="ipt">
  <input type="text" v-model="str">
 </div>
 //word是父元素傳過來的
 <h2>{{ word }}</h2>
 </div>
</template>
<script>
export default {
 name: 'box',
 data() {
 return {
  str: '',
 }
 },
 props: {
 word: ''
 },
 watch: {
 str: function(newValue, oldValue) {
  //每當str的值改變則發(fā)送事件update:word , 并且把值傳過去
  this.$emit('update:word', newValue)
 }
 }
}
</script>
<style scoped></style>

利用了父級可以在子元素上監(jiān)聽子元素的事件

father.vue

<template>
 <div class="hello">
 <input type="text" v-model="wrd">
 <box @incre="boxIncremend" ></box>
 </div>
</template>
<script>
import box from './box'
export default {
 name: 'HelloWorld',
 data() {
 return {
  wrd: ''
 }
 },
 methods: {
 boxIncremend(e) {
  this.wrd = this.wrd + e
 }
 },
 components: {
 box
 }
}
</script>
<style scoped></style>

box.vue

<template>
 <div class="hello">
  <input type="text" v-model="str">
 <h2>{{ word }}</h2>
 </div>
</template>
<script>
export default {
 name: 'box',
 data() {
 return {
  num: 0
 }
 },
 props: {
 word: ''
 },
 watch: {
 str: function(neww, old) {
  //往父級發(fā)射incre事件
  this.$emit('incre', ++this.num)
 }
 },
}
</script>
<style scoped></style>

總結(jié)

以上所述是小編給大家介紹的vue .sync修飾符的使用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 談?wù)刅ue.js——vue-resource全攻略

    談?wù)刅ue.js——vue-resource全攻略

    本篇文章主要介紹了談?wù)刅ue.js——vue-resource全攻略,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • Element-UI介紹主題定制、自定義組件和插件擴展的代碼示例

    Element-UI介紹主題定制、自定義組件和插件擴展的代碼示例

    本文介紹了使用Element-UI實現(xiàn)主題定制、自定義組件和擴展插件的方法和實用案例,在開發(fā)過程中,我們可以根據(jù)自己的需求,靈活選擇相關(guān)的技術(shù)手段,并不斷探索和嘗試,以提高開發(fā)效率和用戶體驗,感興趣的朋友跟隨小編一起看看吧
    2024-02-02
  • Vue3圖片上傳報錯:Required?part?‘file‘?is?not?present.的原因及解決方法

    Vue3圖片上傳報錯:Required?part?‘file‘?is?not?present.的原因及解決方法

    這篇文章主要介紹了Vue3圖片上傳報錯:Required?part?‘file‘?is?not?present.的原因及解決方法,文中通過代碼示例講解的非常詳細,對大家解決問題有一定的幫助,需要的朋友可以參考下
    2024-09-09
  • Vue使用moment將GMT時間轉(zhuǎn)換為北京時間

    Vue使用moment將GMT時間轉(zhuǎn)換為北京時間

    GMT(Greenwich Mean Time)和UTC(Coordinated Universal Time)是兩個時間標準,它們在許多方面非常相似,但也有一些微小的差異,本文給大家詳細介紹了Vue使用moment將GMT時間轉(zhuǎn)換為北京時間,需要的朋友可以參考下
    2023-12-12
  • 詳解vue-cli3多環(huán)境打包配置

    詳解vue-cli3多環(huán)境打包配置

    這篇文章主要介紹了vue-cli3多環(huán)境打包配置,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • Vue中$attrs與$listeners的使用教程

    Vue中$attrs與$listeners的使用教程

    這篇文章主要介紹了Vue中$attrs與$listeners的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-11-11
  • vue項目配置國際化$t('')的介紹和用法示例

    vue項目配置國際化$t('')的介紹和用法示例

    這篇文章主要給大家介紹了關(guān)于vue項目配置國際化?$t('')的介紹和用法的相關(guān)資料,多語言和國際化現(xiàn)在已經(jīng)成為一個網(wǎng)站或應用的必要功能之一,Vue作為一款流行的前端框架,在這方面也有著靈活的解決方案,需要的朋友可以參考下
    2023-09-09
  • Vue實現(xiàn)實時監(jiān)聽頁面寬度高度變化

    Vue實現(xiàn)實時監(jiān)聽頁面寬度高度變化

    這篇文章主要為大家詳細介紹了Vue如何實現(xiàn)實時監(jiān)聽頁面寬度高度變化,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-03-03
  • vue?Proxy數(shù)據(jù)代理進行校驗部分源碼實例解析

    vue?Proxy數(shù)據(jù)代理進行校驗部分源碼實例解析

    Proxy提供了強大的Javascript元編程,有許多功能,包括運算符重載,對象模擬,簡潔而靈活的API創(chuàng)建,對象變化事件,甚至Vue 3背后的內(nèi)部響應系統(tǒng)提供動力,這篇文章主要給大家介紹了關(guān)于vue?Proxy數(shù)據(jù)代理進行校驗部分源碼解析的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • vue中的config目錄下index.js解讀

    vue中的config目錄下index.js解讀

    這篇文章主要介紹了vue中的config目錄下index.js解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03

最新評論

湘阴县| 松潘县| 桐梓县| 台东县| 天镇县| 哈巴河县| 平南县| 梁山县| 乐山市| 凌云县| 利辛县| 同江市| 宽城| 永昌县| 明水县| 贺兰县| 泰宁县| 牙克石市| 渭南市| 开封县| 开封市| 尼玛县| 绍兴县| 治多县| 景德镇市| 天镇县| 华容县| 长武县| 广安市| 高青县| 积石山| 洛扎县| 河曲县| 桃江县| 清涧县| 清苑县| 社旗县| 乌兰察布市| 册亨县| 讷河市| 合山市|