一、在接收頁:
添加引用: private eventManager: JhiEventManager;
發送通知的方法:
下面是自己寫的辦法:
import {Injectable, EventEmitter, OnInit} from "@angular/core";
@Injectable()
export class EmitService implements OnInit {
public eventEmit: any;
constructor() {
// 定義發射事件
this.eventEmit = new EventEmitter();
}
ngOnInit() {
}
}
備注:記得在Module中添加這個service的引用
第二步:
發送通知頁:
import {Component} from '@angular/core';
import {EmitService} from "./emit.service"
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(public emitService: EmitService) {
}
emitFun() {
// 如果組件中,修改了某些數據,需要刷新用用戶列表,用戶列表在其他組件中,那么就可以發射一個字符串過去,那邊接收到這個字符串比對一下,刷新列表。
this.emitService.eventEmit.emit("userList");
}
}
接收通知頁:
import {Component, OnInit} from "@angular/core";
import {EmitService} from "./emit.service"
@Component({
selector: "event-emit",
templateUrl: "./emit.component.html"
})
export class EmitComonent implements OnInit {
constructor(public emitService: EmitService) {
}
ngOnInit() {
// 接收發射過來的數據
this.emitService.eventEmit.subscribe((value: any) => {
if(value == "userList") {
// 這里就可以調取接口,刷新userList列表數據
alert("收到了,我立馬刷新列表");
}
});
}
}