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

VueJs組件之父子通訊的方式

 更新時(shí)間:2018年05月06日 16:01:12   作者:雨點(diǎn)的名字  
這篇文章主要介紹了VueJs組件之父子通訊的方式,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

組件(父子通訊)

一、概括

在一個(gè)組件內(nèi)定義另一個(gè)組件,稱之為父子組件。

   但是要注意的是:1.子組件只能在父組件內(nèi)部使用(寫在父組件tempalte中);

                                2.默認(rèn)情況下,子組件無法訪問父組件上的數(shù)據(jù),每個(gè)組件實(shí)例的作用域是獨(dú)立的;

那如何完成父子如何完成通訊,簡(jiǎn)單一句話:props down, events up :父組件通過 props 向下傳遞數(shù)據(jù)給子組件,子組件通過 events 給父組件發(fā)送

父?jìng)髯樱篜rops
子傳父:子:$emit(eventName) 父$on(eventName)
父訪問子:ref

下面對(duì)三個(gè)進(jìn)行案例講解:

二、父?jìng)髯樱篜rops

     組件實(shí)例的作用域是孤立的。這意味著不能 (也不應(yīng)該) 在子組件的模板內(nèi)直接引用父組件的數(shù)據(jù)。要讓子組件使用父組件的數(shù)據(jù),需要通過子組件的 props 選項(xiàng)

  使用Prop傳遞數(shù)據(jù)包括靜態(tài)和動(dòng)態(tài)兩種形式,下面先介紹靜態(tài)props

1、靜態(tài)props

<script src="https://unpkg.com/vue"></script>
<div id="example">
 <parent></parent>
</div>
<script>
 //要想子組件能夠獲取父組件的,那么在子組件必須申明:props
 var childNode = {
  template: '<div>{{message}}</div>',
  props: ['message']
 }
 //這里的message要和上面props中值一致
 var parentNode = {
  template: `
   <div class="parent">
   <child message="我是"></child>
   <child message="徐小小"></child>
   </div>`,
  components: {
   'child': childNode
  }
 };
 // 創(chuàng)建根實(shí)例
 new Vue({
  el: '#example',
  components: {
   'parent': parentNode
  }
 })
</script>

效果:

 命名約定:

     對(duì)于props聲明的屬性來說,在父級(jí)HTML模板中,屬性名需要使用中劃線寫法

    子級(jí)props屬性聲明時(shí),使用小駝峰或者中劃線寫法都可以;而子級(jí)模板使用從父級(jí)傳來的變量時(shí),需要使用對(duì)應(yīng)的小駝峰寫法

上面這句話什么意思呢?

<script>
 //這里需要注意的是props可以寫成['my-message']或者['myMessage']都是可以的
 //但是template里的屬性名,只能是駝峰式{{myMessage}},如果也寫成{{my-message}}那么是無效的
 var childNode = {
  template: '<div>{{myMessage}}</div>',
  props: ['myMessage']
 }

 //這里的屬性名為my-message
 var parentNode = {
  template: `
   <div class="parent">
   <child my-message="我是"></child>
   <child my-message="徐小小"></child>
   </div>`,
  components: {
   'child': childNode
  }
 };
</script>

       如果我們childNode中的myMessage改成{{my-message}}看運(yùn)行結(jié)果:

2.動(dòng)態(tài)props

    在模板中,要?jiǎng)討B(tài)地綁定父組件的數(shù)據(jù)到子模板的 props,與綁定到任何普通的HTML特性相類似,就是用 v-bind。每當(dāng)父組件的數(shù)據(jù)變化時(shí),該變化也會(huì)傳導(dǎo)給子組件

 var childNode = {
  template: '<div>{{myMessage}}</div>',
  props: ['my-message']
    }

 var parentNode = {
  template: `
 <div class="parent">
 <child :my-message="data1"></child>
 <child :my-message="data2"></child>
 </div>`,
  components: {
   'child': childNode
  },
  data() {
   return {
    'data1': '111',
    'data2': '222'
   }
  }
 };

3、傳遞數(shù)字

初學(xué)者常犯的一個(gè)錯(cuò)誤是使用字面量語法傳遞數(shù)值

<script src="https://unpkg.com/vue"></script>
<div id="example">
 <parent></parent>
</div>
<script>
 var childNode = {
  template: '<div>{{myMessage}}的類型是{{type}}</div>',
  props: ['myMessage'],
  computed: {
   type() {
    return typeof this.myMessage
   }
  }
 }
 var parentNode = {
  template: `
 <div class="parent">
 <my-child my-message="1"></my-child>
 </div>`,
  components: {
   'myChild': childNode
  }
 };
 // 創(chuàng)建根實(shí)例
 new Vue({
  el: '#example',
  components: {
   'parent': parentNode
  }
 })
</script>

結(jié)果:

     因?yàn)樗且粋€(gè)字面 prop,它的值是字符串 "1" 而不是 number。如果想傳遞一個(gè)實(shí)際的 number,需要使用 v-bind,從而讓它的值被當(dāng)作JS表達(dá)式計(jì)算

     如何把String轉(zhuǎn)成number呢,其實(shí)只要改一個(gè)地方。

 var parentNode = {
  template: `
 <div class="parent">
 //只要把父組件my-message="1"改成:my-message="1"結(jié)果就變成number類型
 <my-child :my-message="1"></my-child>
 </div>`,
 };

     當(dāng)然你如果想通過v-bind想傳一個(gè)string類型,那該怎么做呢?

    我們可以使用動(dòng)態(tài)props,在data屬性中設(shè)置對(duì)應(yīng)的數(shù)字1

var parentNode = {
 template: `
 <div class="parent">
 <my-child :my-message="data"></my-child>
 </div>`,
 components: {
 'myChild': childNode
 },
 //這里'data': 1代表就是number類型,'data': "1"那就代表String類型
 data(){
 return {
  'data': 1
 }
 }
};

三、子轉(zhuǎn)父 :$emit

 關(guān)于$emit的用法

   1、父組件可以使用 props 把數(shù)據(jù)傳給子組件。
   2、子組件可以使用 $emit 觸發(fā)父組件的自定義事件。

子主鍵

<template> 
 <div class="train-city"> 
 <span @click='select(`大連`)'>大連</span> 
 </div> 
</template> 
<script> 
export default { 
 name:'trainCity', 
 methods:{ 
 select(val) { 
  let data = { 
  cityname: val 
  }; 
  this.$emit('showCityName',data);//select事件觸發(fā)后,自動(dòng)觸發(fā)showCityName事件 
 } 
 } 
} 
</script> 

父組件

<template> 
 <trainCity @showCityName="updateCity" :index="goOrtoCity"></trainCity> //監(jiān)聽子組件的showCityName事件。 
<template> 
<script> 
export default { 
 name:'index', 
 data () { 
 return { 
  toCity:"北京" 
 } 
 } 
 methods:{ 
 updateCity(data){//觸發(fā)子組件城市選擇-選擇城市的事件 
  this.toCity = data.cityname;//改變了父組件的值 
  console.log('toCity:'+this.toCity)  
 } 
 } 
} 
</script> 

結(jié)果為:toCity: 大連

第二個(gè)案例

<script src="https://unpkg.com/vue"></script>

 <div id="counter-event-example">
  <p>{{ total }}</p>
  <button-counter v-on:increment1="incrementTotal"></button-counter>
  <button-counter v-on:increment2="incrementTotal"></button-counter>
 </div>
<script>
 Vue.component('button-counter', {
  template: '<button v-on:click="increment">{{ counter }}</button>',
  //組件數(shù)據(jù)就是需要函數(shù)式,這樣的目的就是讓每個(gè)button-counter不共享一個(gè)counter
  data: function() {
   return {
    counter: 0
   } 
  },
  methods: {
   increment: function() {
   //這里+1只對(duì)button的值加1,如果要父組件加一,那么就需要$emit事件
    this.counter += 1;
    this.$emit('increment1', [12, 'kkk']);
   }
  }
 });
 new Vue({
  el: '#counter-event-example',
  data: {
   total: 0
  },
  methods: {
   incrementTotal: function(e) {
    this.total += 1;
    console.log(e);
   }
  }
 });
</script>

詳細(xì)講解:

   1:button-counter作為父主鍵,父主鍵里有個(gè)button按鈕。

   2:兩個(gè)button都綁定了click事件,方法里: this.$emit('increment1', [12, 'kkk']);,那么就會(huì)去調(diào)用父類v-on所監(jiān)聽的increment1事件。

   3:當(dāng)increment1事件被監(jiān)聽到,那么執(zhí)行incrementTotal,這個(gè)時(shí)候才會(huì)把值傳到父組件中,并且調(diào)用父類的方法。

   4:這里要注意第二個(gè)button-counter所對(duì)應(yīng)的v-on:'increment2,而它里面的button所對(duì)應(yīng)是this.$emit('increment1', [12, 'kkk']);所以第二個(gè)button按鈕是無法把值傳給他的父主鍵的。

 示例:一個(gè)按鈕點(diǎn)擊一次那么它自身和上面都會(huì)自增1,而第二個(gè)按鈕只會(huì)自己自增,并不影響上面這個(gè)。

還有就是第一個(gè)按鈕每點(diǎn)擊一次,后臺(tái)就會(huì)打印一次如下:

 四、ref ($refs)用法

ref 有三種用法

    1.ref 加在普通的元素上,用this.ref.name 獲取到的是dom元素

    2.ref 加在子組件上,用this.ref.name 獲取到的是組件實(shí)例,可以使用組件的所有方法。

    3.如何利用v-for 和ref 獲取一組數(shù)組或者dom 節(jié)點(diǎn)

1.ref 加在普通的元素上,用this.ref.name 獲取到的是dom元素

<script src="https://unpkg.com/vue"></script>

<div id="ref-outside-component" v-on:click="consoleRef">
 <component-father ref="outsideComponentRef">
 </component-father>
 <p>ref在外面的組件上</p>
</div>
<script>
 var refoutsidecomponentTem = {
  template: "<div class='childComp'><h5>我是子組件</h5></div>"
 };
 var refoutsidecomponent = new Vue({
  el: "#ref-outside-component",
  components: {
   "component-father": refoutsidecomponentTem
  },
  methods: {
   consoleRef: function() {
    console.log(this.); // #ref-outside-component  vue實(shí)例
    console.log(this.$refs.outsideComponentRef); // div.childComp vue實(shí)例
   }
  }
 });
</script>

效果:當(dāng)在div訪問內(nèi)點(diǎn)擊一次:

2.ref使用在外面的元素上

<script src="https://unpkg.com/vue"></script>

<!--ref在外面的元素上-->
<div id="ref-outside-dom" v-on:click="consoleRef">
 <component-father>
 </component-father>
 <p ref="outsideDomRef">ref在外面的元素上</p>
</div>
<script>
 var refoutsidedomTem = {
  template: "<div class='childComp'><h5>我是子組件</h5></div>"
 };
 var refoutsidedom = new Vue({
  el: "#ref-outside-dom",
  components: {
   "component-father": refoutsidedomTem
  },
  methods: {
   consoleRef: function() {
    console.log(this); // #ref-outside-dom vue實(shí)例
    console.log(this.$refs.outsideDomRef); // <p> ref在外面的元素上</p>
   }
  }
 });
</script>

 效果:當(dāng)在div訪問內(nèi)點(diǎn)擊一次:

3.ref使用在里面的元素上---局部注冊(cè)組件

<script src="https://unpkg.com/vue"></script>
<!--ref在里面的元素上-->
<div id="ref-inside-dom">
 <component-father>
 </component-father>
 <p>ref在里面的元素上</p>
</div>
<script>
 var refinsidedomTem = {
  template: "<div class='childComp' v-on:click='consoleRef'>" +
   "<h5 ref='insideDomRef'>我是子組件</h5>" +
   "</div>",
  methods: {
   consoleRef: function() {
    console.log(this); // div.childComp vue實(shí)例 
    console.log(this.$refs.insideDomRef); // <h5 >我是子組件</h5>
   }
  }
 };
 var refinsidedom = new Vue({
  el: "#ref-inside-dom",
  components: {
   "component-father": refinsidedomTem
  }
 });
</script>

  效果:當(dāng)在click范圍內(nèi)點(diǎn)擊一次:

4.ref使用在里面的元素上---全局注冊(cè)組件

<script src="https://unpkg.com/vue"></script>
<!--ref在里面的元素上--全局注冊(cè)-->
<div id="ref-inside-dom-all">
 <ref-inside-dom-quanjv></ref-inside-dom-quanjv>
</div>
<script>
 //v-on:input指當(dāng)input里值發(fā)生改變觸發(fā)showinsideDomRef事件
 Vue.component("ref-inside-dom-quanjv", {
  template: "<div class='insideFather'> " +
   "<input type='text' ref='insideDomRefAll' v-on:input='showinsideDomRef'>" +
   " <p>ref在里面的元素上--全局注冊(cè) </p> " +
   "</div>",
  methods: {
   showinsideDomRef: function() {
    console.log(this); //這里的this其實(shí)還是div.insideFather
    console.log(this.$refs.insideDomRefAll); // <input type="text">
   }
  }
 });
 var refinsidedomall = new Vue({
  el: "#ref-inside-dom-all"
 });
</script>

效果:當(dāng)我第一次輸入1時(shí),值已改變出發(fā)事件,當(dāng)我第二次在輸入時(shí)在觸發(fā)一次事件,所以后臺(tái)應(yīng)該打印兩次

總結(jié)

以上所述是小編給大家介紹的VueJs組件之父子通訊的方式,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎各我留言,小編會(huì)及時(shí)回復(fù)大家的!

相關(guān)文章

  • vue2.0多條件搜索組件使用詳解

    vue2.0多條件搜索組件使用詳解

    這篇文章主要為大家詳細(xì)介紹了vue2.0多條件搜索組件的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • 淺析vue-router jquery和params傳參(接收參數(shù))$router $route的區(qū)別

    淺析vue-router jquery和params傳參(接收參數(shù))$router $route的區(qū)別

    今天做項(xiàng)目時(shí)踩到了vue-router傳參的坑(jquery和params),所以決定總結(jié)一下二者的區(qū)別。感興趣的朋友跟隨腳本之家小編一起看看吧
    2018-08-08
  • idea部署RuoYi-Vue分離版的圖文詳解

    idea部署RuoYi-Vue分離版的圖文詳解

    RuoYi-Vue是一款基于SpringBoot+Vue的前后端分離極速后臺(tái)開發(fā)框架, 本文主要介紹了idea部署RuoYi-Vue分離版的圖文詳解,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-06-06
  • vue設(shè)置一開始進(jìn)入的頁面教程

    vue設(shè)置一開始進(jìn)入的頁面教程

    今天小編就為大家分享一篇vue設(shè)置一開始進(jìn)入的頁面教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • Vue中設(shè)置登錄驗(yàn)證攔截功能的思路詳解

    Vue中設(shè)置登錄驗(yàn)證攔截功能的思路詳解

    今天在做vue和springboot交互的一個(gè)項(xiàng)目的時(shí)候,想要基于前端實(shí)現(xiàn)一些只有登錄驗(yàn)證之后才能訪問某些頁面的操作,所以在這里總結(jié)一下實(shí)現(xiàn)該功能的一個(gè)解決方案
    2021-10-10
  • vue  自定義組件實(shí)現(xiàn)通訊錄功能

    vue 自定義組件實(shí)現(xiàn)通訊錄功能

    本文通過實(shí)例代碼給介紹了vue使用自定義組件實(shí)現(xiàn)通訊錄功能,需要的朋友可以參考下
    2018-09-09
  • nuxtjs中如何對(duì)axios二次封裝

    nuxtjs中如何對(duì)axios二次封裝

    這篇文章主要介紹了nuxtjs中如何對(duì)axios二次封裝問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue-cli4創(chuàng)建項(xiàng)目導(dǎo)入Element-UI踩過的坑及解決

    vue-cli4創(chuàng)建項(xiàng)目導(dǎo)入Element-UI踩過的坑及解決

    這篇文章主要介紹了vue-cli4創(chuàng)建項(xiàng)目導(dǎo)入Element-UI踩過的坑及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • vue3手動(dòng)刪除keepAlive緩存的方法

    vue3手動(dòng)刪除keepAlive緩存的方法

    當(dāng)我們未設(shè)置keepAlive的最大緩存數(shù)時(shí),當(dāng)緩存組件太多,會(huì)導(dǎo)致內(nèi)存溢出,本文給大家介紹了vue3手動(dòng)刪除keepAlive緩存的方法,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2024-03-03
  • vue 通過綁定事件獲取當(dāng)前行的id操作

    vue 通過綁定事件獲取當(dāng)前行的id操作

    這篇文章主要介紹了vue 通過綁定事件獲取當(dāng)前行的id操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07

最新評(píng)論