欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue父子傳遞實(shí)例講解

 更新時(shí)間:2020年02月14日 15:34:43   投稿:laozhang  
在本篇文章里小編給大家整理的是關(guān)于Vue父子傳遞實(shí)例講解,需要的朋友們可以跟著學(xué)習(xí)參考下。

實(shí)現(xiàn)功能:

1、子組件的input輸入,改變父組件信息

2、父組件對(duì)子組件1,3進(jìn)行監(jiān)聽與控制

3、子組件1與子組件3相互關(guān)聯(lián)

父子雙向通信流程:

子組件的input通過(guò)事件監(jiān)聽->控制子組件的data中變量/向父組件$emit子事件及變量

父組件監(jiān)聽到$emit事件及變量,賦予父組件的變量,通過(guò)props傳到子組件

<body>
<div id = "app">
 <!-- //將父組件num1的數(shù)據(jù)傳給props中的pnumber1,實(shí)現(xiàn)父?jìng)髯?
 //監(jiān)聽子組件num1change事件,這里recdnum1函數(shù),沒(méi)明白加括號(hào)出錯(cuò) -->
<cnp 
 :pnumber1 = "num1"
 :pnumber3 = "num3"
 @num1change = "recdnum1"
 @num3change="recdnum3" >
</cnp>
</div> 

<!-- //template 編寫自己的模版 -->
<template id = "mytemp">
 <div>
  <h3>props:{{pnumber1}}</h3>
  <h3>child-data:{{dnumber1}}</h3>
  <h3></h3>
  <input type="text" :value="dnumber1" @input="num1input">
  <h3>props:{{pnumber3}}</h3>
  <h3>child-data:{{dnumber3}}</h3>
  <input type="text" :value="dnumber3" @input="num3input">  
 </div>
</template>

<script>
var app = new Vue({
 el:"#app",
 data(){
  return{
   //父組件數(shù)據(jù)庫(kù)中的默認(rèn)數(shù)值
   num1:11,
   num3:31,
  }
 },
 methods:{
  //監(jiān)聽事件中,將子組件傳來(lái)的dnumber1傳給num1,num1傳給pnumber1
  recdnum1(value){
   //默認(rèn)傳遞來(lái)的value為String,根據(jù)需要轉(zhuǎn)為Int或Float
   this.num1 = parseInt(value);
   },
  recdnum3(value){
   this.num3=parseFloat(value);
   }   
 },
 components: {
  cnp:{
   template:"#mytemp",
   props: {
    pnumber1:{
     //默認(rèn)props的數(shù)據(jù)格式和默認(rèn)值
     type:Number,
     default:1
    },
    pnumber3:{
     type:Number,
     default:3
    }        
   },
   data(){
    return{
     //將props的數(shù)值賦予data中變量
     dnumber1:this.pnumber1,
     dnumber3:this.pnumber3
    }
   },
   methods: {
    num1input(event){
     //將input中的數(shù)值傳給dnumber1
     this.dnumber1 = event.target.value;
     //向父組件發(fā)射事件num1change,并傳出dnumber1數(shù)據(jù)
     this.$emit("num1change",this.dnumber1);
     //綁定pnumber3數(shù)值的關(guān)聯(lián)性
     this.dnumber3=this.dnumber1*5;
     //向父組件發(fā)射pnumber3信息
     this.$emit("num3change",this.dnumber3);
    },
    num3input(event){
     this.dnumber3 = event.target.value;
     this.$emit("num3change",this.dnumber3)
    },
   }
  }
 }
});
</script>
</body>

以上就是相關(guān)的實(shí)例代碼,希望腳本之家整理的內(nèi)容能夠幫助到大家。

相關(guān)文章

最新評(píng)論