自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

前端框架Vue—父子組件數(shù)據(jù)雙向綁定

開發(fā) 前端
Vue項(xiàng)目中經(jīng)常使用到組件之間的數(shù)值傳遞,實(shí)現(xiàn)的方法很多,但是原理上基本大同小異。父向子組件傳值,子組件接收到數(shù)據(jù)之后,保存到自己的變量中。

[[423412]]

 Vue項(xiàng)目中經(jīng)常使用到組件之間的數(shù)值傳遞,實(shí)現(xiàn)的方法很多,但是原理上基本大同小異。

實(shí)現(xiàn)思路:

父 向 子 組件傳值:使用 props 屬性。( props 是property[屬性] 的復(fù)數(shù)簡(jiǎn)寫 )

子 向 父 組件傳值:使用自定義事件。

一、父子組件單向傳值

1.1、父向子傳值

父向子組件傳值,子組件接收到數(shù)據(jù)之后,保存到自己的變量中。

  1. //父組件寫法 
  2. <cld :numP="num" ></cld> 
  3.  
  4. //子組件定義以及數(shù)據(jù) 
  5. components:{ 
  6.  cld:{ 
  7.   template:'#child'
  8.   props:{ 
  9.    numP:Number 
  10.   }, 
  11.  } 
  12.  
  13. //子組件內(nèi)容 
  14. <template id="child"
  15.  <div> 
  16.   {{ numP }} 
  17.  </div> 
  18. </template> 

 props 用于接收父組件傳過來的值,props 的寫法有很多種,具體如:

  1. //方式1 :  直接接收數(shù)據(jù) 
  2. props: [ 'numP' ] 
  3.  
  4. //方式2: 加類型限制 
  5. props: [ 
  6.  numP: Number 
  7.  ]  
  8.  
  9. //方式3:添加默認(rèn)值 
  10. props: [ 
  11.  numP: { 
  12.   type:Number, 
  13.   default:0 
  14.   } 
  15. ]  
  16.  
  17. //方式4:是否必須值限制 
  18. props: [ 
  19.  numP: { 
  20.   type:Number, 
  21.   default:0, 
  22.   require:true //添加必須值,不傳此值會(huì)報(bào)錯(cuò) 
  23.  } 
  24. ]  
  25.  
  26. //方式5:采用對(duì)象形式 
  27. props: { 
  28.  numP: { 
  29.   type:Number, 
  30.   default:0, 
  31.  } 

1.2、子向父?jìng)髦?/h3>

子向父組件傳值,主要通過自定義事件進(jìn)行傳值,具體實(shí)例如下:

  1. // 父組件內(nèi)容 
  2. <div> 
  3.  子組件獲取到的數(shù)據(jù){{getNum}} 
  4.  <cld :numb="num" @accept="getNumC"></cld> 
  5. </div> 
  6.  
  7. //父組件方法 
  8. methods:{ 
  9.  getNumC(data){ 
  10.   this.getNum = data //接收子組件傳的數(shù)據(jù) 
  11.  } 
  12. }, 
  13. //子組件定義 
  14. components:{ 
  15.  cld:{ 
  16.   template:'#child'
  17.   data(){ 
  18.    return
  19.     numC:1314 //子組件數(shù)據(jù)定義 
  20.    } 
  21.   }, 
  22.   mounted(){ 
  23.     this.$emit( 'accept' , this.numC ) // 觸發(fā)自定義事件 
  24.    } 
  25.   } 
  26. }, 

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

Vue 的數(shù)據(jù)都是單向流動(dòng)的,而且 vue 中從來就沒有任何的雙向綁定,v-model 實(shí)現(xiàn)的雙向綁定只是語法糖而已。

方式1:利用 watch 實(shí)現(xiàn)父子組件的數(shù)據(jù)雙向綁定,具體實(shí)例如下:

  1. <div id="app"
  2.  數(shù)據(jù)<br>{{num}} 
  3.  <input type="text" v-model="num"><br> 
  4.  <cld :numb="num" @accept="getNumC"></cld> 
  5. </div> 
  6. //子組件內(nèi)容 
  7. <template id="child"
  8.  <div> 
  9.   數(shù)據(jù)<br>{{childNum}} 
  10.   <input type="text" v-model="childNum" /> 
  11.  </div> 
  12. </template> 
  13.  
  14.   <!-- 父子組件通信 --> 
  15. const app = new Vue({ 
  16.  el:'#app'
  17.   data:{ 
  18.    num:'520'
  19.    }, 
  20.   methods:{ 
  21.    getNumC(data){ 
  22.     this.num = data 
  23.    } 
  24.   }, 
  25.   components:{ 
  26.    cld:{ 
  27.     template:'#child'
  28.     props:{ 
  29.      numb:String 
  30.     }, 
  31.    data(){ 
  32.     return
  33.      childNum:0, 
  34.     } 
  35.    }, 
  36.   watch:{ 
  37.    numb:function(){ 
  38.     this.childNum = this.numb 
  39.    }, 
  40.    childNum:function(){ 
  41.     this.$emit('accept',this.childNum) 
  42.     } 
  43.    }, 
  44.   mounted(){ 
  45.    this.childNum = this.numb 
  46.    } 
  47.   } 
  48.  }  
  49. }) 

方式2:.sync 修飾符實(shí)現(xiàn)雙向綁定

在vue 1.x 中的 .sync 修飾符所提供的功能。當(dāng)一個(gè)子組件改變了一個(gè)帶 .sync 的 prop 的值時(shí),這個(gè)變化也會(huì)同步到父組件中所綁定的值。這很方便,但也會(huì)導(dǎo)致問題,因?yàn)樗茐牧藛蜗驍?shù)據(jù)流。(數(shù)據(jù)自上而下流,事件自下而上走)

  1. <cld :numb.sync="num" ></cld> 
  2. //會(huì)擴(kuò)展為: 
  3. <cld  :numb="bar"  @update:numb=”val => bar = val”/> 

當(dāng)組件需要更新 numb 的值時(shí),需要觸發(fā)更新事件:

  1. this.$emit("update:numb", newValue ); 

使用具體實(shí)例如下:

  1. // 父組件 
  2. <Father :foo.sync="foo"></Father> 
  3.  
  4. //子組件 
  5. props: ['foo'], 
  6. data() { 
  7.   return { 
  8.    newFoo: this.foo; 
  9.    } 
  10. }, 
  11. methods:{ 
  12.  add:function(){ 
  13.   this.newMsg=10; 
  14.   this.$emit('update:foo',this.newFoo); 
  15.  } 

 

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2021-09-15 08:09:43

前端技術(shù)編程

2022-08-22 09:01:24

Vue響應(yīng)式原則雙向數(shù)據(jù)綁定

2023-04-18 09:17:40

父子組件Vue

2021-02-19 23:07:02

Vue綁定組件

2020-09-12 16:22:27

Vue

2022-09-02 10:34:23

數(shù)據(jù)Vue

2017-10-27 22:03:35

javascrip

2021-04-02 11:24:22

Vue2.x雙向綁定前端

2022-07-27 08:40:06

父子組件VUE3

2024-01-09 08:34:56

Vue3.js組件通信

2016-12-27 15:23:56

vue.js雙向綁定操作

2021-11-11 23:16:33

前端數(shù)據(jù)格式Web

2023-09-05 23:29:49

前端Vue

2019-05-29 14:23:53

Vue.js組件通信

2017-08-08 09:15:41

前端JavaScript頁面渲染

2020-07-28 07:27:07

Vue前端Jquery

2021-08-23 10:14:20

鴻蒙HarmonyOS應(yīng)用

2022-01-25 18:11:55

vdomclassfunction

2017-09-27 16:44:23

前端

2023-04-27 08:23:38

JavaScriptVue.jsMVVC
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)