angular2--不同页面间发起通知 sfnBroadCast

例如:当我退出登录的时候,需要通知用户中心

一、在需要发起通知的页面:

1、引入

import { EventServices } from '../../../services/event.services';

2、在构造函数中加入:

constructor(private eventBus: EventServices){}

3、点击退出的时候,发起通知

let param = {
	eventType: "USERINFO",    //标识,在接收通知的时候要用到,可自定义
	eventData: null                    //想要传递的参数,可自定义
};
this.eventBus.SfyBroadCast(JSON.stringify(param));

二、需要接收通知的页面

1、引入

import {EventServices} from '../../../services/event.services';
import {Subscription} from 'rxjs';

2、创建变量

subscription: Subscription;

3、在构造函数加入

constructor(private eventBus: EventServices){}

4、接收通知

this.subscription = eventBus.SfyBroadCast$.subscribe(
	event => {
		let result = JSON.parse(event);
		if(result.eventType == "USERINFO") {
			console.log(result);
			//可做其他操作  result.eventData.XXX
		}
	}, error => {}
);

猜你喜欢

转载自blog.csdn.net/Ariel_201311/article/details/85265128
今日推荐