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

首頁 > 編程 > JavaScript > 正文

vuejs事件中心管理組件間的通信詳解

2019-11-19 15:51:23
字體:
來源:轉載
供稿:網友

本文為大家分享了vuejs事件中心管理組件間的通信,供大家參考,具體內容如下

事件中心

這個可以是一個空的全局的Vue實例,其他的組件利用這個實例emit和on自定義事件,這樣組件定義了自己的事件處理方法。

import Vue from 'Vue'window.eventHub = new Vue();

事件監聽和注銷監聽

事件監聽應在更組件的created鉤子函數中進行,在組件銷毀前應注銷事件監聽

 //hook  created: function () { //listen event window.eventHub.$on('switchComments',this.switchComments); window.eventHub.$on('removeIssue',this.removeIssue); window.eventHub.$on('saveComment',this.saveComment); window.eventHub.$on('removeComment',this.removeComment); //get init data var that =this; axios.get('issue/index') .then(function (resp) {  that.issue_list=resp.data; }); }, beforeDestroy: function () { window.eventHub.$off('switchComments'); window.eventHub.$off('removeIssue'); window.eventHub.$off('saveComment'); window.eventHub.$off('removeComment'); }

子組件的emit事件,注意這里用的window.$emit而不是this.emit

 methods: { removeComment: function(index,cindex) {  window.eventHub.$emit('removeComment', {index:index, cindex:cindex}); }, saveComment: function(index) {  window.eventHub.$emit('saveComment', {index: index, comment: this.comment});  this.comment=""; } },

Note: 這其實還不是最理想的通信方式,下一篇我們看看vuex怎么玩

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜兰县| 铁力市| 合山市| 静海县| 浦北县| 濮阳市| 天峨县| 普格县| 连江县| 健康| 峨眉山市| 仪陇县| 平安县| 观塘区| 商南县| 宾阳县| 肥城市| 灵丘县| 应用必备| 乌什县| 全南县| 西吉县| 缙云县| 高安市| 策勒县| 辛集市| 东乡| 普陀区| 九台市| 定远县| 红河县| 东莞市| 垫江县| 汉寿县| 阜平县| 叙永县| 奉节县| 凌云县| 阳谷县| 绥滨县| 鄂州市|