欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解vue 組件之間使用eventbus傳值

 更新時間:2017年10月25日 11:42:47   作者:HUSHILIN001  
本篇文章主要介紹了vue 組件之間使用eventbus傳值。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

對于前端的我們而言,并非是只有寫界面才是最大的問題,很多的情況下,我們需要關(guān)注的是數(shù)據(jù),比如js頁面的數(shù)據(jù)傳遞等等,學(xué)習(xí)vue我們也是需要知道怎么去使用數(shù)據(jù)

當(dāng)然,使用存儲也是可以得,但是并非一定要緩存,當(dāng)然在vue中有推薦了我們?nèi)ナ褂胿uex去數(shù)據(jù)交互,Vuex會讓你的Vue代碼足夠靈活可控,把數(shù)據(jù)統(tǒng)一存入state, 只允許通過Actions觸發(fā)Mutations修改。然而,有時候我們的項目并沒有復(fù)雜到需要用上Vuex。,(我們也不討論已經(jīng)廢除的vm.$dispatch)很多情況下我們都是需要一個事件的捕獲,這時候我們就可以用到vue的eventbus了

受用eventbus的方法很是簡單,我們需要做三步事情,第一步,我們需要創(chuàng)造一個容器去充當(dāng)我們的eventbus

第二步,我們需要去拋出,或者說提交我們的事件

第三步,我們?nèi)ケO(jiān)聽我們的那個事件(也許這才是第二部)

首先,我們需要在全局定義我們的eventbus

這里我們定義到了eventbus。這就簡單的完成了我們的第一步,當(dāng)然,全局變量,我想你應(yīng)該知道定義在哪兒的

接著我們先去拋出這個事件,使用¥。emit去“提交”

怎樣,這點都可以理解吧,其次我們經(jīng)行第三步,去監(jiān)聽

當(dāng)然。這里已經(jīng)監(jiān)聽好的。點擊事件俺只是個累贅,

接下來我們就要去界面中使用它們了

首先,倒入我們所需要的文件:


這里我使用的是談transimissionone還有transimissiontwo兩個文件‘

接著是定義

其次是使用

最后運行我們的項目,查看下效果

這邊主要是交大家使用,所以代碼就俘虜在下面,主要是四個文件

transimissionone。vue(發(fā)送事件的文件)

<template> 
  <div class="transimission1"> 
  <button @click="get">點擊發(fā)送數(shù)值到eventbus中</button>  
  </div> 
   
</template> 
 
<script> 
  export default { 
    name: "transimission1", 
    methods: { 
      get: function() { 
        console.log("Aaa"); 
        eventBus.$emit('eventBusName', "hellokugou"); 
      } 
    }, 
  } 
</script> 
 
<style> 
 
</style> 

其次是transimissiontwo(監(jiān)聽者)

<template> 
  <div class="transimissiontwo"> 
    <button @click="method1">點擊console.log出eventbus的信息 
</button> 
  </div> 
</template> 
 
<script> 
  export default { 
    name: "transimissiontwo", 
    methods: { 
      method1: function() { 
        //使用on老監(jiān)聽事件 
        eventBus.$on('eventBusName', function(val) {  
          console.log("這個是用transimissiontwo的val值為:"+val) 
        }) 
      } 
    } 
  } 
</script> 
<style> 
 
</style> 

接著是我們的中樞。app。vue中使用

<template> 
  <div id="app"> 
    <click></click> 
  <transimissiontwo></transimissiontwo> 
    <transimissionone></transimissionone> 
  <sendparent @listenertochildevent="getmessagefromchild"></sendparent> 
    <value :locallogo="netlogo"></value> 
    <!--無法監(jiān)聽,說明要在那個組件中--> 
    <button @listenertochildevent="getmessagefromchild">測試能否監(jiān)聽</button> 
    <my_plug_in></my_plug_in> 
    <div class="choose_div"> 
      <ul> 
 
        <li> 
          <router-link to="/foo">foo頁面</router-link> 
        </li> 
        <li> 
          <router-link to="/header">header頁面</router-link> 
        </li> 
        <li> 
          <router-link to="/hello">hello頁面</router-link> 
        </li> 
        <li style="clear: both;list-style: none;"></li> 
      </ul> 
 
    </div> 
 
    <div class="main"> 
      <router-view class="my_router_iew"></router-view> 
    </div> 
    <testmintui></testmintui> 
  </div> 
</template> 
 
<script> 
  import value from './components/value' 
  import click from "./components/click" 
  import my_plug_in from "./components/plug_in" 
  import sendparent from "./components/send_parent" 
  import testmintui from "./components/Test_mint-ui" 
  import transimissiontwo from "./components/transimissiontwo" 
  import transimissionone from "./components/transimissionone" 
 
  export default { 
    name: 'app', 
    data() { 
      return { 
        netlogo: "主頁顯示信息到組件中" 
      } 
    }, 
    components: { 
      value, 
      click, 
      my_plug_in, 
      sendparent, 
      testmintui, 
      transimissionone, 
    transimissiontwo, 
     
    }, 
    methods: { 
      getmessagefromchild: function(data) { 
        console.log(data); 
      } 
    } 
  } 
</script> 
 
<style> 
  body { 
    background-color: #f8f8ff; 
    font-family: 'Avenir', Helvetica, Arial, sans-serif; 
    color: #2c3e50; 
  } 
   
  ul { 
    width: 12rem; 
  } 
   
  ul li { 
    list-style: none; 
  } 
   
  ul li:not(:last-child) { 
    list-style: none; 
    width: 2rem; 
    margin-left: 0.1rem; 
    margin-right: 0.1rem; 
    float: left; 
    text-align: center; 
    background: #2C3E50; 
    color: white; 
  } 
   
  ul li a { 
    text-decoration: none; 
    font-size: 16px; 
    color: white; 
    line-height: 1rem; 
    text-align: center; 
  } 
   
  ul li:nth-child { 
    list-style: none; 
    clear: both; 
  } 
   
  .choose_div { 
    width: 100%; 
    overflow: scroll; 
  } 
</style> 

請無視掉沒用的代碼。接著就是定義eventbus了

window.eventBus = new Vue();

就這樣,很是簡單,當(dāng)然,對于級別的可以使用prop,下回再講

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

相關(guān)文章

  • Element中table組件(el-table)右側(cè)滾動條空白占位處理

    Element中table組件(el-table)右側(cè)滾動條空白占位處理

    當(dāng)我設(shè)置了max-height,就會在表格右側(cè)出現(xiàn)一列空白的占位,本文主要介紹了Element中table組件(el-table)右側(cè)滾動條空白占位處理,感興趣的可以了解一下
    2023-09-09
  • el-tree文字顯示不全的解決辦法

    el-tree文字顯示不全的解決辦法

    本文主要介紹了el-tree文字顯示不全的解決辦法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • vue3實現(xiàn)移動端滑動模塊

    vue3實現(xiàn)移動端滑動模塊

    這篇文章主要為大家詳細(xì)介紹了vue3實現(xiàn)移動端滑動模塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • vue中如何獲取當(dāng)前路由地址

    vue中如何獲取當(dāng)前路由地址

    這篇文章主要介紹了vue中如何獲取當(dāng)前路由地址,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • VUE搭建分布式醫(yī)療掛號系統(tǒng)的前臺預(yù)約掛號步驟詳情

    VUE搭建分布式醫(yī)療掛號系統(tǒng)的前臺預(yù)約掛號步驟詳情

    這篇文章主要介紹了VUE搭建分布式醫(yī)療掛號系統(tǒng)的前臺預(yù)約掛號步驟詳情,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-04-04
  • vue3??mixin?混入使用方法

    vue3??mixin?混入使用方法

    這篇文章主要介紹了?vue3??mixin?混入使用方法,mixin?混入,提供了一種非常靈活的方式,來分發(fā)?vue?組件中的可復(fù)用功能,一個mixin?對象可以包含任意組件選項,當(dāng)組件使用?mixin?對象時,所有的?mixin?對象的選項將被混入組件本身的選項。,需要的朋友可以參考一下
    2021-11-11
  • vue+iview實現(xiàn)文件上傳

    vue+iview實現(xiàn)文件上傳

    這篇文章主要為大家詳細(xì)介紹了vue+iview實現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • Vue鍵盤事件用法總結(jié)

    Vue鍵盤事件用法總結(jié)

    本篇文章主要介紹了Vue鍵盤事件用法總結(jié),詳細(xì)的介紹了各種鍵盤事件的用法,有興趣的可以了解一下
    2017-04-04
  • Vue3中使用富文本編輯器的示例詳解

    Vue3中使用富文本編輯器的示例詳解

    有不少的前端需求都需要使用到富文本編輯器,所以這篇文章主要來和大家介紹一下如何在Vue3項目中使用富文本編輯器,感興趣的可以了解下
    2024-04-04
  • 在vue中利用v-html按分號將文本換行的例子

    在vue中利用v-html按分號將文本換行的例子

    今天小編就為大家分享一篇在vue中利用v-html按分號將文本換行的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11

最新評論