亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

VueJs組件之父子通訊的方式

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

組件(父子通訊)

一、概括

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

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

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

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

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

下面對三個進行案例講解:

二、父傳子:Props

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

  使用Prop傳遞數(shù)據(jù)包括靜態(tài)和動態(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)建根實例
 new Vue({
  el: '#example',
  components: {
   'parent': parentNode
  }
 })
</script>

效果:

 命名約定:

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

    子級props屬性聲明時,使用小駝峰或者中劃線寫法都可以;而子級模板使用從父級傳來的變量時,需要使用對應的小駝峰寫法

上面這句話什么意思呢?

<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}}看運行結果:

2.動態(tài)props

    在模板中,要動態(tài)地綁定父組件的數(shù)據(jù)到子模板的 props,與綁定到任何普通的HTML特性相類似,就是用 v-bind。每當父組件的數(shù)據(jù)變化時,該變化也會傳導給子組件

 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ù)字

初學者常犯的一個錯誤是使用字面量語法傳遞數(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)建根實例
 new Vue({
  el: '#example',
  components: {
   'parent': parentNode
  }
 })
</script>

結果:

     因為它是一個字面 prop,它的值是字符串 "1" 而不是 number。如果想傳遞一個實際的 number,需要使用 v-bind,從而讓它的值被當作JS表達式計算

     如何把String轉成number呢,其實只要改一個地方。

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

     當然你如果想通過v-bind想傳一個string類型,那該怎么做呢?

    我們可以使用動態(tài)props,在data屬性中設置對應的數(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
 }
 }
};

三、子轉父 :$emit

 關于$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ā)后,自動觸發(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> 

結果為:toCity: 大連

第二個案例

<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ù)式,這樣的目的就是讓每個button-counter不共享一個counter
  data: function() {
   return {
    counter: 0
   } 
  },
  methods: {
   increment: function() {
   //這里+1只對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>

詳細講解:

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

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

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

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

 示例:一個按鈕點擊一次那么它自身和上面都會自增1,而第二個按鈕只會自己自增,并不影響上面這個。

還有就是第一個按鈕每點擊一次,后臺就會打印一次如下:

 四、ref ($refs)用法

ref 有三種用法

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

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

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

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實例
    console.log(this.$refs.outsideComponentRef); // div.childComp vue實例
   }
  }
 });
</script>

效果:當在div訪問內(nèi)點擊一次:

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實例
    console.log(this.$refs.outsideDomRef); // <p> ref在外面的元素上</p>
   }
  }
 });
</script>

 效果:當在div訪問內(nèi)點擊一次:

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

<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實例 
    console.log(this.$refs.insideDomRef); // <h5 >我是子組件</h5>
   }
  }
 };
 var refinsidedom = new Vue({
  el: "#ref-inside-dom",
  components: {
   "component-father": refinsidedomTem
  }
 });
</script>

  效果:當在click范圍內(nèi)點擊一次:

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

<script src="https://unpkg.com/vue"></script>
<!--ref在里面的元素上--全局注冊-->
<div id="ref-inside-dom-all">
 <ref-inside-dom-quanjv></ref-inside-dom-quanjv>
</div>
<script>
 //v-on:input指當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在里面的元素上--全局注冊 </p> " +
   "</div>",
  methods: {
   showinsideDomRef: function() {
    console.log(this); //這里的this其實還是div.insideFather
    console.log(this.$refs.insideDomRefAll); // <input type="text">
   }
  }
 });
 var refinsidedomall = new Vue({
  el: "#ref-inside-dom-all"
 });
</script>

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

總結

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

相關文章

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

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

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

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

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

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

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

    vue設置一開始進入的頁面教程

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

    Vue中設置登錄驗證攔截功能的思路詳解

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

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

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

    nuxtjs中如何對axios二次封裝

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

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

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

    vue3手動刪除keepAlive緩存的方法

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

    vue 通過綁定事件獲取當前行的id操作

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

最新評論