子組件與父組件通信,$emit ,$on

 我們在使用vue做項目的時候,通信的必不可少的,我們一般都是使用$emit,$on來通信,
 比如在組件A中點擊一個事件,觸發(fā)事件后,返回的數(shù)據(jù)要在B組件中展示。
A組件
<ul class="storm-wrap" v-show="stormlist.length!=0">
          <li class="storm-item" v-on:click="sendstorm(item)" v-for="item in stormlist" :url="item.url">            
                  {{item.name}}
            </li>
</ul>
methods: {
 sendstorm: function (item) {
       var vm = this;
       this.$emit('sendstorm', item.url)
 },
}
B組件
<div id="main" v-cloak v-loading.fullscreen.lock="fullscreenLoading">
   <storm-item v-on:sendstorm="geturl"></storm-item>
</div>
 methods: {
     geturl: function (url) {
         showimg(url)
      },
}
    在A組件中$emit一個事件,可以傳送參數(shù),在b組件引用的時候,給組件綁定傳過來的事件名,執(zhí)行的方法是在B組件定義好的
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容