天天看點

ionic4 監聽事件

監聽事件我是這麼了解的:程度等待一個事件的發生,當這個事件發生之後,對它作出反應。比如更換頭像,當我更換完頭像之後,個人中心就要重新擷取最新的頭像。

那麼在ionic4中,我們如何實作監聽事件呢?

1、安裝監聽事件包

cnpm install --save eventemitter3
           

2、在事件發生頁面,引入監聽元件,聲明變量,發出廣播

//引入元件
import { EventService } from './app/services/event.service';
//在constructior裡聲明監聽變量
public eventService: EventService,
//通知個人中心更換頭像,和tab5更換頭像
this.eventService.event.emit('getnewphoto');
           

3、在監聽界面,同樣引入監聽元件,聲明變量,同上。另外,在對應方法添加監聽事件。

//監聽
this.eventService.event.on('getnewphoto',()=>{
   this.getnewphoto();
})
           

注意:監聽的方法要一緻。

繼續閱讀