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

Vue EventBus自定義組件事件傳遞

 更新時(shí)間:2018年06月25日 09:24:42   作者:丨大麥  
這篇文章主要介紹了Vue EventBus自定義組件事件傳遞,組件化應(yīng)用構(gòu)建是Vue的特點(diǎn)之一,本文主要介紹EventBus進(jìn)行數(shù)據(jù)消息傳遞 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

前言

組件化應(yīng)用構(gòu)建是Vue的特點(diǎn)之一,因此我們在Vue的實(shí)際開發(fā)過程中會(huì)經(jīng)常需要封裝自定義組件,以提高開發(fā)的效率。 而組件在大部分情況下并不會(huì)孤立的存在,它必然會(huì)與父組件和兄弟組件產(chǎn)生數(shù)據(jù)的交互。所以在這里為大家總結(jié)兩種組件數(shù)據(jù)交互的方式:EventBus和利用Vuex框架進(jìn)行狀態(tài)管理。

我會(huì)通過兩種不同的交互方式,它們對于父子組件間數(shù)據(jù)交互和兄弟組件間數(shù)據(jù)交互。

由于篇幅關(guān)系,本文主要介紹EventBus進(jìn)行數(shù)據(jù)消息傳遞;關(guān)于運(yùn)用Vuex框架進(jìn)行狀態(tài)管理在下一篇文章中介紹。

案例介紹

本章節(jié)會(huì)有大量的代碼示例,為了讓讀者閱讀輕松,做如下目錄和組件介紹。本章節(jié)主要運(yùn)用了兩個(gè)子組件和一個(gè)父組件。

子組件文件名:SearchInput.vueSearchItem.vue

父組件文件名:StateView.vue

目錄結(jié)構(gòu)展示:

1、SearchInput.vue

組件介紹:一個(gè)輸入框,它會(huì)onInput方法去監(jiān)聽輸入內(nèi)容,并調(diào)用方法,將輸入框內(nèi)的數(shù)據(jù)傳遞出去。

代碼展示:

<template>
 <div>
 <input placeholder="搜索內(nèi)容" v-model="searchContent"/>
 </div>
</template>

<script type="text/ecmascript-6">
 export default{
 data(){
  return{
  searchContent:""
  }
 },
 props:{

 }
 }
</script>

<style lang="stylus" rel="stylesheet/stylus">

</style>

SearchItem.vue

組件介紹:一個(gè)span,它主要用來接收父組件傳遞的內(nèi)容和接收同胞組件輸入框傳遞的內(nèi)容,并進(jìn)行展示。

代碼示例:

<template>
 <span class="item">
  {{content}}
 </span>
</template>

<script type="text/ecmascript-6">
 export default{
 data(){
  return{
  content:this.itemContent
  }
 },
 props:{
  itemContent:{
  type:String,
  required:true
  }
 }
 }
</script>

<style lang="stylus" rel="stylesheet/stylus">
 .item
 background #f4f4f4
 padding 3px 5px
 box-sizing border-box
 display inline-block
 cursor pointer
</style>

StateView.vue

組件介紹:父組件,主要展示頁面和加載子組件

代碼示例:

<template>
 <div>
 <search-view></search-view>
 <div>
  <search-item :itemContent="content"/>
  <search-item itemContent="熱門搜索2"/>
  <search-item itemContent="熱門搜索3"/>
 </div>
 <div>{{content}}</div>

 </div>
</template>

<script type="text/ecmascript-6">
import searchView from '../components/SearchInput.vue'
import searchItem from '../components/SearchItem.vue'

export default{
 data () {
 return {
  content:"接收輸入框的值"
 }
 },
 components: {
 searchView,
 searchItem
 }
}

</script>

<style lang="stylus" rel="stylesheet/stylus">

</style>

正文

EventBus

1、父組件發(fā)送數(shù)據(jù)給子組件,可以通過子組件定義的 props 自定義屬性,去傳遞數(shù)據(jù)

2、EventBus其實(shí)就是通過實(shí)例化一個(gè)Vue實(shí)例,然后通過該實(shí)例的 $emit 方法發(fā)送數(shù)據(jù)消息和 $on 方法接收數(shù)據(jù)消息。它適用在子組件發(fā)送消息給父組件或子組件發(fā)送消息給兄弟組件。

我們看下一個(gè)下面案例主要展示了:

1、通過 props 父組件(StateView)去為子組件(SearchItem)傳遞數(shù)據(jù);

2、子組件(SearchInput)通過 EventBus 和父組件(StateView)、兄弟組件(SearchItem)傳遞數(shù)據(jù);

目錄結(jié)構(gòu)展示

 

效果展示

代碼展示:(粗體表示變化部分)

 1、第一步:自定義一個(gè)EventBus(SearchEvent.js)

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

在這里我們 new 了一個(gè)Vue的實(shí)例,并將它輸出。

第二步:子組件通過EventBus發(fā)送數(shù)據(jù)消息

<template>
 <div>
 <input placeholder="搜索內(nèi)容" @input="sendEvent" v-model="searchContent"/> //增加了@input=“sendEvent”,去監(jiān)聽onInput事件,并回調(diào)sendEvent方法
 </div>
</template>

<script type="text/ecmascript-6">
 import searchEvent from '../event/SearchEvent'  //導(dǎo)入EventBus
 export default{
 data(){
  return{
  searchContent:""
  }
 },
 methods:{
  sendEvent:function(){  //定義sendEvent方法,在input中監(jiān)聽onInput,并回調(diào)該方法
   /**
   * 通過導(dǎo)入的searchEvent調(diào)用$emit方法去發(fā)送數(shù)據(jù)消息。
   * 第一個(gè)參數(shù)為事件名,到時(shí)候我們要通過該事件名去接收數(shù)
   * 第二個(gè)參數(shù)為數(shù)據(jù)值,如果只有一個(gè)參數(shù),我們可以直接傳遞該參數(shù)
   * 如果有兩個(gè)及以上的參數(shù),我們可以通過對象的形式去傳遞。
   */
  searchEvent.$emit("search",this.searchContent)
  //多個(gè)參數(shù)傳遞的示例:
  //searchEvent.$emit("search",{"content":this.searchContent,"valTwo":"valTow"})
  }
 },
 props:{

 }
 }
</script>

<style lang="stylus" rel="stylesheet/stylus">

</style>

在上面的示例我們主要做了以下事情: 1、導(dǎo)入EventBus

2、通過 @input="sendEvent" 去監(jiān)聽 onInput 事件,并發(fā)現(xiàn)輸入框內(nèi)內(nèi)容有改變時(shí),回調(diào) sendEvent 方法,調(diào)用 EventBus.emit() 方法把數(shù)據(jù)消息發(fā)送出去

第三步父組件(StateView)和子組件(SearchItem)接收數(shù)據(jù)消息

StateView.vue

<template>
 <div>
 <search-view></search-view>
 <div>
  <search-item :itemContent="content"/> //通過props去為子組件傳遞(動(dòng)態(tài)數(shù)據(jù):content)數(shù)據(jù)
  <search-item itemContent="熱門搜索2"/> //通過props去為子組件傳遞(固定數(shù)據(jù):熱門搜索2)數(shù)據(jù)
  <search-item itemContent="熱門搜索3"/>
 </div>
 <div>{{content}}</div>

 </div>
</template>

<script type="text/ecmascript-6">
import searchView from '../components/SearchInput.vue'
import searchItem from '../components/SearchItem.vue'
import searchEvent from '../event/SearchEvent' //導(dǎo)入EventBus
export default{
 data () {
 return {
  content:"接收輸入框的值"
 }
 },
 mounted(){
 /**
  * 在mounted接受數(shù)據(jù)消息,$on接受兩個(gè)參數(shù)。
  * 第一個(gè)參數(shù)是消息事件名,應(yīng)該與發(fā)送數(shù)據(jù)消息的第一個(gè)參數(shù)相同,否則接收不到數(shù)據(jù)消息
  * 第二個(gè)參數(shù)是一個(gè)函數(shù),對數(shù)據(jù)消息事件做處理;該函數(shù)帶一個(gè)參數(shù),則是數(shù)據(jù)。
  */
 searchEvent.$on('search',(val)=>{
  this.content=val;
  //示例:如果數(shù)據(jù)傳遞的是對象形式
  // this.content=val.content;
 })
 },
 components: {
 searchView,
 searchItem
 }
}

</script>

<style lang="stylus" rel="stylesheet/stylus">

</style>

在上面的示例我們主要做了以下事情:

1、在父組件,我們通過SearchItem的 props 去傳遞了數(shù)據(jù)。

2、通過在組件 mounted 生命周期中調(diào)用 EventBus.on() 方法去接收子組件(SearchInput)的數(shù)據(jù)消息,并對content進(jìn)行修改值

SearchItem.vue

<template>
 <span class="item">
  {{content}}
 </span>
</template>

<script type="text/ecmascript-6">
 import searchEvent from '../event/SearchEvent' //導(dǎo)入EventBus
 export default{
 data(){
  return{
  content:this.itemContent
  }
 },
 props:{
  itemContent:{
  type:String,
  required:true
  }
 },
 mounted(){
 /**
  * 在mounted接受數(shù)據(jù)消息,$on接受兩個(gè)參數(shù)。
  * 第一個(gè)參數(shù)是消息事件名,應(yīng)該與發(fā)送數(shù)據(jù)消息的第一個(gè)參數(shù)相同,否則接收不到數(shù)據(jù)消息
  * 第二個(gè)參數(shù)是一個(gè)函數(shù),對數(shù)據(jù)消息事件做處理;該函數(shù)帶一個(gè)參數(shù),則是數(shù)據(jù)。
  */
  searchEvent.$on('search',(val)=>{
  this.content=val;
  })
 }
 }
</script>

<style lang="stylus" rel="stylesheet/stylus">
 .item
 background #f4f4f4
 padding 3px 5px
 box-sizing border-box
 display inline-block
 cursor pointer
</style>

在上面的示例我們主要做了一事情:

通過在組件 mounted 生命周期中調(diào)用 EventBus.on() 方法去接收子組件(SearchInput)的數(shù)據(jù)消息,并對content進(jìn)行修改值。

我們可以感受到 SearchInput一發(fā)送數(shù)據(jù)消息,所有注冊接收 search 事件的地方都會(huì)接收到數(shù)據(jù)消息

復(fù)盤:

1、父組件給子組件進(jìn)行數(shù)據(jù)傳遞可以通過 props 進(jìn)行傳遞。

2、子組件給父組件進(jìn)行消息傳遞或子組件給兄弟組件進(jìn)行消息傳遞可以通過EventBus去實(shí)例化一個(gè)Vue,并通過該實(shí)例的 $emit$on 方法去傳遞和接收數(shù)據(jù)消息。

3、數(shù)據(jù)消息一旦發(fā)送,所有注冊了接收該數(shù)據(jù)消息的地方都會(huì)接收到該數(shù)據(jù)消息。

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

相關(guān)文章

  • Vue報(bào)錯(cuò):Injection?"xxxx"?not?found的解決辦法

    Vue報(bào)錯(cuò):Injection?"xxxx"?not?found的解決辦法

    這篇文章主要給大家介紹了關(guān)于Vue報(bào)錯(cuò):Injection?"xxxx"?not?found的解決辦法,文中通過圖文將解決的辦法介紹的非常詳細(xì),對大家的學(xué)習(xí)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • Vue3項(xiàng)目頁面實(shí)現(xiàn)echarts圖表漸變色的動(dòng)態(tài)配置的實(shí)現(xiàn)步驟

    Vue3項(xiàng)目頁面實(shí)現(xiàn)echarts圖表漸變色的動(dòng)態(tài)配置的實(shí)現(xiàn)步驟

    在開發(fā)可配置業(yè)務(wù)平臺(tái)時(shí),需要實(shí)現(xiàn)讓用戶對項(xiàng)目內(nèi)echarts圖表的動(dòng)態(tài)配置,讓用戶脫離代碼也能實(shí)現(xiàn)簡單的圖表樣式配置,顏色作為圖表樣式的重要組成部分,其配置方式是項(xiàng)目要解決的重點(diǎn)問題,所以本文介紹了Vue3項(xiàng)目頁面實(shí)現(xiàn)echarts圖表漸變色的動(dòng)態(tài)配置
    2024-10-10
  • Vue3中事件總線mitt的使用方式

    Vue3中事件總線mitt的使用方式

    mitt又稱事務(wù)總線,是第三方插件,這篇文章主要為大家詳細(xì)介紹了Vue3中事件總線mitt的使用方式,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-03-03
  • 詳解Vue底部導(dǎo)航欄組件

    詳解Vue底部導(dǎo)航欄組件

    這篇文章主要介紹了Vue底部導(dǎo)航欄的詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • 關(guān)于vue3默認(rèn)把所有onSomething當(dāng)作v-on事件綁定的思考

    關(guān)于vue3默認(rèn)把所有onSomething當(dāng)作v-on事件綁定的思考

    這篇文章主要介紹了關(guān)于vue3默認(rèn)把所有`onSomething`當(dāng)作`v-on`事件綁定的思考,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Vue3使用pinia進(jìn)行數(shù)據(jù)添加、修改和刪除的操作代碼

    Vue3使用pinia進(jìn)行數(shù)據(jù)添加、修改和刪除的操作代碼

    Pinia?是?Vue?3?的官方狀態(tài)管理庫,旨在提供一種簡單、靈活且類型安全的狀態(tài)管理解決方案,Pinia?的設(shè)計(jì)理念與?Vuex?類似,但更加輕量且易于使用,文旨在全面解析?Vue?3?中如何使用?Pinia?進(jìn)行數(shù)據(jù)的添加、修改和刪除,需要的朋友可以參考下
    2025-03-03
  • vue使用節(jié)流函數(shù)的踩坑實(shí)例指南

    vue使用節(jié)流函數(shù)的踩坑實(shí)例指南

    防抖和節(jié)流的目的都是為了減少不必要的計(jì)算,下面這篇文章主要給大家介紹了關(guān)于vue使用節(jié)流函數(shù)踩坑的相關(guān)資料,需要的朋友可以參考下
    2021-05-05
  • Vue前端打包的詳細(xì)流程

    Vue前端打包的詳細(xì)流程

    這篇文章主要介紹了Vue前端打包的詳細(xì)流程,下面文章圍繞Vue前端打包的相關(guān)資料展開詳細(xì)內(nèi)容,需要的小伙伴可以參考一下,希望對大家有所幫助
    2021-11-11
  • vue窗口變化onresize詳解

    vue窗口變化onresize詳解

    這篇文章主要介紹了vue窗口變化onresize,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 在Vue中解決跨域問題的常用方式

    在Vue中解決跨域問題的常用方式

    跨域問題是由瀏覽器引起的安全限制,而不是Vue框架本身導(dǎo)致的,Vue本身并不限制跨域訪問,它只是一個(gè)前端框架,負(fù)責(zé)構(gòu)建用戶界面和處理數(shù)據(jù)邏輯,本文給大家介紹了在Vue中解決跨域問題的常用方式,需要的朋友可以參考下
    2023-10-10

最新評(píng)論

黔西县| 鹤峰县| 金沙县| 东安县| 万荣县| 文安县| 南京市| 安福县| 明溪县| 嘉荫县| 乃东县| 凤城市| 英超| 宜兴市| 岚皋县| 鹤山市| 中宁县| 枣庄市| 天气| 清流县| 宣威市| 新巴尔虎左旗| 错那县| 琼中| 霍州市| 永城市| 同心县| 迭部县| 乌海市| 满洲里市| 图们市| 常熟市| 桦南县| 东丽区| 波密县| 九龙坡区| 香格里拉县| 桐庐县| 宁蒗| 申扎县| 洛宁县|