這里用的還是訂閱者模式
深入理解Angular訂閱者模式
我在項目當(dāng)中用的是那個subject,在A頁面操作成功發(fā)生一個通知到B頁面
// rest.service.ts
send(message: any) {
this.subject.next(message);
}
get(): Observable<any> {
return this.subject.asObservable();
}
// A頁面發(fā)送消息 left.component.ts
handleRoute(name) {
this.router.navigate(['/' + name],{queryParams:{temp:this.temp}}); // 路由傳參
// 通過 activedRoute 的snapShot的queryParams["temp"]接收參數(shù)或者是
//接收多個參數(shù)
constructor(
private activatedRoute: ActivatedRoute, //這里需要注入ActivatedRoute模塊
) {
activatedRoute.queryParams.subscribe(queryParams => {
let productId = queryParams.productId;
let title = queryParams.title;
});
this.restService.send('who are you ?') // 發(fā)送消息
}
// B頁面接收消息 upcoming-process.component.ts
ngOnInit() {
// 監(jiān)聽頁面?zhèn)鬟^來的值
this.restService.get().subscribe((result) => {
this.search(); // 接收消息
})
}