angular 组件传递消息(通过服务)

angular 组件传递消息(通过服务)

import {Injectable} from '@angular/core';
import {Observable} from "rxjs/Observable";
import {Subject} from "rxjs/Rx";

@Injectable()
export class MenuService {

private subject = new Subject<any>();


/**
* 设置消息
*/
sendMenu(type: string) {
this.subject.next(type);
}

/**
* 清理消息
*/
clearMenu() {
this.subject.next();
}

/**
* 获得消息
*/
getMenu(): Observable<any> {
return this.subject.asObservable();
}

/*-----Constructor Part-----*/

constructor() {

}

/*-----Methods Part-----*/

get() {
let menu = sessionStorage.getItem('childIndex');
return menu;
}

set(menu: string) {
sessionStorage.setItem('childIndex', menu);
this.sendMenu(menu);
}


}

你可能感兴趣的:(angular)