日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
前端框架Vue—父子組件數(shù)據(jù)雙向綁定

 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.  
  3.  
  4. //子組件定義以及數(shù)據(jù) 
  5. components:{ 
  6.  cld:{ 
  7.   template:'#child', 
  8.   props:{ 
  9.    numP:Number 
  10.   }, 
  11.  } 
  12.  
  13. //子組件內(nèi)容 
  14.  
  15.  
     
  16.   {{ numP }} 
  17.  
 
  •  
  •  props 用于接收父組件傳過(guò)來(lái)的值,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>

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

     
     
     
    1. // 父組件內(nèi)容 
    2.  
    3.  子組件獲取到的數(shù)據(jù){{getNum}} 
    4.   

     
  •  
  • //父組件方法 
  • methods:{ 
  •  getNumC(data){ 
  •   this.getNum = data //接收子組件傳的數(shù)據(jù) 
  •  } 
  • }, 
  • //子組件定義 
  • components:{ 
  •  cld:{ 
  •   template:'#child', 
  •   data(){ 
  •    return{ 
  •     numC:1314 //子組件數(shù)據(jù)定義 
  •    } 
  •   }, 
  •   mounted(){ 
  •     this.$emit( 'accept' , this.numC ) // 觸發(fā)自定義事件 
  •    } 
  •   } 
  • }, 
  • 二、父子組件數(shù)據(jù)雙向綁定

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

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

      
     
     
    1.  
    2.  數(shù)據(jù)
      {{num}} 
    3.  
       
    4.   
     
  • //子組件內(nèi)容 
  •  
  •  
     
  •   數(shù)據(jù)
    {{childNum}} 
  •    
  •  
  •  
  •  
  •  
  •    
  • const app = new Vue({ 
  •  el:'#app', 
  •   data:{ 
  •    num:'520', 
  •    }, 
  •   methods:{ 
  •    getNumC(data){ 
  •     this.num = data 
  •    } 
  •   }, 
  •   components:{ 
  •    cld:{ 
  •     template:'#child', 
  •     props:{ 
  •      numb:String 
  •     }, 
  •    data(){ 
  •     return{ 
  •      childNum:0, 
  •     } 
  •    }, 
  •   watch:{ 
  •    numb:function(){ 
  •     this.childNum = this.numb 
  •    }, 
  •    childNum:function(){ 
  •     this.$emit('accept',this.childNum) 
  •     } 
  •    }, 
  •   mounted(){ 
  •    this.childNum = this.numb 
  •    } 
  •   } 
  •  }  
  • }) 
  • 方式2:.sync 修飾符實(shí)現(xiàn)雙向綁定

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

     
     
     
    1.  
    2. //會(huì)擴(kuò)展為: 
    3.  bar = val”/> 

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

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

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

     
     
     
    1. // 父組件 
    2.  
    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.  } 

    網(wǎng)頁(yè)名稱:前端框架Vue—父子組件數(shù)據(jù)雙向綁定
    新聞來(lái)源:http://www.5511xx.com/article/cohshcj.html