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

Vue中this.$emit和this.$on的使用

 更新時(shí)間:2024年03月24日 10:07:43   作者:易安sparkle  
這篇文章主要介紹了Vue中this.$emit和this.$on的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

1、解決第一次監(jiān)聽不到數(shù)據(jù)的方法

  • this.$nextTick()
this.$nextTick(function () {
     this.$bus.$emit("yulan", row); 
 })
  • setTimeout
setTimeout(() => {
     //row 代表要傳遞的參數(shù)數(shù)據(jù)
     this.$bus.$emit("yulan", row); 
}, 100);

2、子組件傳值給父組件

子組件中使用$emit觸發(fā)事件的值傳出去,父組件通過事件監(jiān)聽,獲取數(shù)據(jù);

兩種情況

由子組件中操作何時(shí)傳值

(解決:在子組件中定義一個(gè)click事件觸發(fā)自定義事件$emit,在父組件監(jiān)聽)

// 父組件:parent.vue
<template>
  <div>
    <child @childevent='wathChildEvent'></child>
    <div>子組件的數(shù)據(jù)為:{{msg}}</div>
  </div>
</template>
<script>
import child from "./child";
export default {
  data(){
    return{
      msg:""
    }
  },
  components:{
    child
  },
  methods:{
    wathChildEvent:function(val){//直接監(jiān)聽 又子組件觸發(fā)的事件,參數(shù)為子組件的傳來的數(shù)據(jù)
      console.log(val);   //子組件的數(shù)據(jù),將有子組件操作觸發(fā)傳給父組件
      this.msg = val;
    } 
  }
}
</script>

// 子組件:child.vue
<template>
  <div><input type="button" value="子組件觸發(fā)" @click="target"></div>
</template>
<script>
export default {
  data(){
      return {
          texts:'這是子組件的數(shù)據(jù),將有子組件操作觸發(fā)傳給父組件'
      }
  },
  methods:{
    target:function(){ //有子組件的事件觸發(fā) 自定義事件childevent
      this.$emit('childevent',this.texts);//觸發(fā)一個(gè)在子組件中聲明的事件 childEvnet
    }
  },
}
</script>

由父組件中操作何時(shí)傳值

(解決:在父組件中第一一個(gè)click點(diǎn)擊事件,在組件中通過refs獲取實(shí)例方法來直接觸發(fā)事件,然后在父組件中監(jiān)聽)

// 父組件:parent.vue
<template>
  <div>
    <child @childevent='wathChildEvent' ref="childcomp"></child>
    <input type="button" @click="parentEnvet" value="父組件觸發(fā)" />
    <div>子組件的數(shù)據(jù)為:{{msg}}</div>
  </div>
</template>
<script>
import child from "./child";
export default {
  data(){
    return{
      msg:""
    }
  },
  components:{
    child
  },
  methods:{
    wathChildEvent:function(val){//直接監(jiān)聽 又子組件觸發(fā)的事件,參數(shù)為子組件的傳來的數(shù)據(jù)
      console.log(val);//子組件的數(shù)據(jù),將有子組件操作觸發(fā)傳給父組件
      this.msg = val;
    },
    parentEnvet:function(){
      this.$refs['childcomp'].target(); //通過refs屬性獲取子組件實(shí)例,又父組件操作子組件的方法觸發(fā)事件$meit
    }
  }
}
</script>

// 子組件:child.vue
<template>
  <div> dothing </div>
</template>
<script>
export default {
  data(){
      return {
          texts:'這是子組件的數(shù)據(jù),將有子組件操作觸發(fā)傳給父組件'
      }
  },
  methods:{
    target:function(){ //又子組件的事件觸發(fā) 自定義事件childevent
      this.$emit('childevent',this.texts);//觸發(fā)一個(gè)在子組件中聲明的事件 childEvnet
    }
  },
}
</script>

3、兄弟組件間傳值

  • 傳遞數(shù)據(jù)方–發(fā)射事件

通過事件傳遞$emit(方法名,傳遞的數(shù)據(jù))

  • 接收數(shù)據(jù)方–監(jiān)聽事件

在mounted()中觸發(fā)$on(方法名,callback(接收數(shù)據(jù)的數(shù)據(jù)))

// 建立一個(gè)空的Vue實(shí)例,將通信事件掛載在該實(shí)例上
// emptyVue.js
import Vue from 'vue'
export default new Vue()

// 兄弟組件a:childa.vue
<template>
  <div>
    <span>A組件->{{msg}}</span>
    <input type="button" value="把a(bǔ)組件數(shù)據(jù)傳給b" @click ="send">
  </div>
</template>
<script>
import vmson from "./emptyVue"
export default {
  data(){
    return {
      msg:"我是a組件的數(shù)據(jù)"
    }
  },
  methods:{
    send:function(){
      vmson.$emit("aevent",this.msg)
    }
  }
}
</script>

// 兄弟組件b:childb.vue
<template>
   <div>
    <span>b組件,a傳的的數(shù)據(jù)為->{{msg}}</span>
  </div>
</template>
<script>
import vmson from "./emptyVue"
export default {
 data(){
    return {
      msg:""
    }
  },
  mounted(){
    vmson.$on("aevent",(val)=>{//監(jiān)聽事件aevent,回調(diào)函數(shù)要使用箭頭函數(shù);
      console.log(val);//打印結(jié)果:我是a組件的數(shù)據(jù)
      this.msg = val;
    })
  }
}
</script>

// 父組件:parent.vue
<template>
  <div>
    <childa><childa>
    <childb></childb>
  </div>
</template>
<script>
import childa from "./childa";
import childb from "./childb";
export default {
  data(){
    return{
      msg:""
    }
  },
  components:{
    childa,
    childb
  },
}
</script>

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue下canvas裁剪圖片實(shí)例講解

    vue下canvas裁剪圖片實(shí)例講解

    在本篇文章里小編給大家整理了關(guān)于vue下canvas裁剪圖片實(shí)例講解內(nèi)容,需要的朋友們可以參考下。
    2020-04-04
  • 基于Vue sessionStorage實(shí)現(xiàn)保留搜索框搜索內(nèi)容

    基于Vue sessionStorage實(shí)現(xiàn)保留搜索框搜索內(nèi)容

    這篇文章主要介紹了基于Vue sessionStorage實(shí)現(xiàn)保留搜索框搜索內(nèi)容,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Vue.js使用$.ajax和vue-resource實(shí)現(xiàn)OAuth的注冊(cè)、登錄、注銷和API調(diào)用

    Vue.js使用$.ajax和vue-resource實(shí)現(xiàn)OAuth的注冊(cè)、登錄、注銷和API調(diào)用

    這篇文章主要介紹了 Vue.js使用$.ajax和vue-resource實(shí)現(xiàn)OAuth的注冊(cè)、登錄、注銷和API調(diào)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • vue 中的keep-alive實(shí)例代碼

    vue 中的keep-alive實(shí)例代碼

    這篇文章主要介紹了vue中的keep-alive實(shí)例代碼,vue實(shí)現(xiàn)組件信息緩存的方法,在文中也給大家提到,需要的朋友可以參考下
    2018-07-07
  • vue點(diǎn)擊自增和求和的實(shí)例代碼

    vue點(diǎn)擊自增和求和的實(shí)例代碼

    今天小編就為大家分享一篇vue點(diǎn)擊自增和求和的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • vue使用lottie-web實(shí)現(xiàn)web動(dòng)畫效果

    vue使用lottie-web實(shí)現(xiàn)web動(dòng)畫效果

    在web端,lottie-web庫可以解析導(dǎo)出的動(dòng)畫json文件,并將其以svg或者canvas的方式將動(dòng)畫繪制在我們的頁面上,這篇文章主要介紹了vue使用lottie-web實(shí)現(xiàn)web動(dòng)畫,需要的朋友可以參考下
    2024-06-06
  • 學(xué)習(xí)Vite的原理

    學(xué)習(xí)Vite的原理

    這篇文章主要介紹了Vite的原理,Vite是一個(gè)更輕、更快的web應(yīng)用開發(fā)工具,面向現(xiàn)代瀏覽,Vite創(chuàng)建的項(xiàng)目是一個(gè)普通的Vue3應(yīng)用,相比基于Vue-cli創(chuàng)建的應(yīng)用少了很多配置文件和依賴,下面基于Vite相關(guān)資料內(nèi)容,需要的朋友可以參考一下
    2022-02-02
  • vue-router如何實(shí)現(xiàn)history模式配置

    vue-router如何實(shí)現(xiàn)history模式配置

    這篇文章主要介紹了vue-router如何實(shí)現(xiàn)history模式配置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 圖文介紹Vue父組件向子組件傳值

    圖文介紹Vue父組件向子組件傳值

    本文通過實(shí)例代碼給大家詳細(xì)分享了Vue父組件向子組件傳值的過程以及關(guān)鍵點(diǎn)講解,一起學(xué)習(xí)參考下吧。
    2018-02-02
  • vue使用微信JS-SDK實(shí)現(xiàn)分享功能

    vue使用微信JS-SDK實(shí)現(xiàn)分享功能

    這篇文章主要介紹了vue使用微信JS-SDK實(shí)現(xiàn)分享功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08

最新評(píng)論

沂水县| 五原县| 舒兰市| 彰化县| 平罗县| 云梦县| 清远市| 洛浦县| 西城区| 台南县| 宁强县| 防城港市| 吉林省| 江源县| 铁力市| 兴城市| 滦平县| 日土县| 龙胜| 太和县| 甘洛县| 淅川县| 静乐县| 鄯善县| 南涧| 阿图什市| 连城县| 峨山| 冷水江市| 平昌县| 宜黄县| 酉阳| 东海县| 元谋县| 浑源县| 阳东县| 灵台县| 红原县| 平凉市| 深泽县| 郓城县|