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

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ú)立的;

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

父傳子:Props
子傳父:子:$emit(eventName) 父$on(eventName)
父訪問子:ref

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

二、父傳子:Props

     組件實(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)文章

  • 一文帶你深入理解Vue3響應(yīng)式原理

    一文帶你深入理解Vue3響應(yīng)式原理

    響應(yīng)式就是當(dāng)對(duì)象本身(對(duì)象的增刪值)或者對(duì)象屬性(重新賦值)發(fā)生變化時(shí),將會(huì)運(yùn)行一些函數(shù),最常見的就是render函數(shù),下面這篇文章主要給大家介紹了關(guān)于Vue3響應(yīng)式原理的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • Vue3中使用vuex4的實(shí)現(xiàn)示例

    Vue3中使用vuex4的實(shí)現(xiàn)示例

    本文主要介紹了Vue3中使用vuex4的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • 基于vue cli 通過命令行傳參實(shí)現(xiàn)多環(huán)境配置

    基于vue cli 通過命令行傳參實(shí)現(xiàn)多環(huán)境配置

    這篇文章主要介紹了vue項(xiàng)目通過命令行傳參實(shí)現(xiàn)多環(huán)境配置(基于@vue/cli)的相關(guān)資料,需要的朋友可以參考下
    2018-07-07
  • Vue.js實(shí)現(xiàn)開發(fā)購物車功能的方法詳解

    Vue.js實(shí)現(xiàn)開發(fā)購物車功能的方法詳解

    這篇文章主要介紹了Vue.js實(shí)現(xiàn)開發(fā)購物車功能的方法,結(jié)合實(shí)例形式分析了基于vue.js開發(fā)的購物車功能相關(guān)操作步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-02-02
  • Vue.js刷新當(dāng)前頁面的常見方法

    Vue.js刷新當(dāng)前頁面的常見方法

    這篇文章主要介紹了Vue.js刷新當(dāng)前頁面的常見方法,文中通過代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-12-12
  • Vue滾動(dòng)到指定位置的多種方式示例詳解

    Vue滾動(dòng)到指定位置的多種方式示例詳解

    當(dāng)容器有滾動(dòng)條時(shí),有時(shí)需要將指定的內(nèi)容自動(dòng)滾動(dòng)到可視區(qū)域,怎么實(shí)現(xiàn)呢,下面小編給大家?guī)砹硕喾N方法實(shí)現(xiàn)Vue滾動(dòng)到指定位置,感興趣的朋友跟隨小編一起看看吧
    2023-08-08
  • 關(guān)于vue v-for 循環(huán)問題(一行顯示四個(gè),每一行的最右邊那個(gè)計(jì)算屬性)

    關(guān)于vue v-for 循環(huán)問題(一行顯示四個(gè),每一行的最右邊那個(gè)計(jì)算屬性)

    這篇文章主要介紹了關(guān)于vue v-for 循環(huán)問題(一行顯示四個(gè),每一行的最右邊那個(gè)計(jì)算屬性),需要的朋友可以參考下
    2018-09-09
  • 詳解vue如何使用rules對(duì)表單字段進(jìn)行校驗(yàn)

    詳解vue如何使用rules對(duì)表單字段進(jìn)行校驗(yàn)

    這篇文章主要介紹了詳解vue如何使用rules對(duì)表單字段進(jìn)行校驗(yàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-10-10
  • vue實(shí)現(xiàn)錨點(diǎn)跳轉(zhuǎn)之scrollIntoView()方法詳解

    vue實(shí)現(xiàn)錨點(diǎn)跳轉(zhuǎn)之scrollIntoView()方法詳解

    這篇文章主要介紹了vue實(shí)現(xiàn)錨點(diǎn)跳轉(zhuǎn)之scrollIntoView()方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Vue3 插槽使用匯總

    Vue3 插槽使用匯總

    這篇文章主要給大家分享了Vue3的 插槽使用匯總,在 2.6.0中,vue 為具名插槽和作用于插槽引入了一個(gè)新的統(tǒng)一語法:v-slot。它取代了 slot 和 slot-scope 在新版中的應(yīng)用,下面就一起來看看文章的詳細(xì)內(nèi)容吧
    2021-12-12

最新評(píng)論

盐池县| 天峻县| 五指山市| 上栗县| 巴南区| 文昌市| 曲水县| 元谋县| 景宁| 蒙城县| 山丹县| 依兰县| 蒙阴县| 堆龙德庆县| 同江市| 建湖县| 仪征市| 繁昌县| 玛曲县| 枞阳县| 邯郸市| 济南市| 离岛区| 博兴县| 巩留县| 金阳县| 轮台县| 保山市| 色达县| 淳化县| 达州市| 会泽县| 阆中市| 黔江区| 集安市| 邯郸市| 文昌市| 杭锦旗| 达孜县| 太原市| 澄城县|