国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

vue中$refs, $emit, $on, $once, $off的使用詳解

2019-11-19 11:28:17
字體:
供稿:網(wǎng)友

1.$refs的使用場景

父組件調(diào)用子組件的方法,可以傳遞數(shù)據(jù)。

父組件:

<div id="app">  <child-a ref="child"></child-a>  <button @click="getMyEvent">點擊父組件</button><div><script>  import ChildA from './child.vue'    export default{    components:{      ChildA    },    data(){      return {        msg:'我是父組件的數(shù)據(jù)'      }    },    methods:{      getMyEvent(){        //調(diào)用子組件的方法,child是上邊ref起的名字,emitEvent是子組件的方法。        this.$refs.child.emitEvent(this.msg)      }    }  }</script>

子組件:

<template>  <button>點擊我</button></template><script>  export default{    methods:{      emitEvent(msg){        console.log('接收的數(shù)據(jù)------'+msg)      }    }  }</script>

2.$emit的使用

子組件調(diào)用父組件的方法并傳遞數(shù)據(jù)。

子組件:

<template>  <button @click="emitEvent">點擊我</button></template><script>  export default{    data(){      return{        msg:'我是子組件的數(shù)據(jù)'      }    },    methods:{      emitEvent(){        //通過按鈕的點擊事件觸發(fā)方法,然后用$emit觸發(fā)一個my-event的自定義方法,傳遞this.msg數(shù)據(jù)。        this.$emit('my-event',this.msg)      }    }  }</script>

父組件:

<template>    <div id="app">    <child-a @my-event="getMyEvent"></child-a>    //父組件通過監(jiān)測my-event事件執(zhí)行一個方法,然后取到子組件中傳遞過來的值。  </div></template><script>  import childA from './child.vue';  export default {    components:{      childA    },    methods:{      getMyEvent(msg){       console.log('接收數(shù)據(jù)---'+msg);       //接收數(shù)據(jù),我是子組件的數(shù)據(jù)      }    }  }</script>

3.$on的使用場景

兄弟組件之間相互傳遞數(shù)據(jù)。

首先創(chuàng)建一個Vue的空白實例(兄弟組件的橋梁)

import Vue from 'vue';export default new Vue();

子組件A:發(fā)送放使用$emit自定義事件把數(shù)據(jù)帶過去。

<template>  <div>    <span>A組件-{{msg}}</span>    <input type="button" value="把A組件數(shù)據(jù)傳遞給B" @click="send">  </div></template><script>  import eventBus from './eventBus';  export default{    data(){      return{        msg:{          a:'111',          b:'222'        }      }    },    methods:{      send(){        eventBus.$emit('aevent',this.msg)      }    }  }</script>

子組件B:接收方通過$on監(jiān)聽自定義事件的callback接收數(shù)據(jù)

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

父組件:

<template>    <div>    <childa></childa>    <br/>    <childb></childb>  </div></template><script>  import childa from './childa.vue';  import childb from './childb.vue';  export default{    componets:{      childa,      childb    },    data(){      return{        msg:''      }    }  }</script>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 纳雍县| 古交市| 江城| 哈密市| 宜昌市| 木里| 收藏| 缙云县| 苏州市| 长海县| 白山市| 江油市| 手游| 綦江县| 德惠市| 霸州市| 昭通市| 眉山市| 怀集县| 长兴县| 前郭尔| 平遥县| 仁布县| 平江县| 凤山市| 若羌县| 通山县| 房山区| 忻州市| 德庆县| 遂昌县| 南漳县| 湟中县| 和平区| 金塔县| 滕州市| 延川县| 邹平县| 遵义市| 南京市| 邹平县|