私が観察LOGGEDINを定義するログインサービスがあります。
private loggedIn$: Subject<LoggedIn>;
constructor() {
this.loggedIn$ = <Subject<LoggedIn>>new Subject();
}
私はその後、設定した2つのメソッドを定義し得る:
setLoggedIn(loggedIn: LoggedIn): void {
this.loggedIn$.next(loggedIn);
}
getLoggedIn(): Observable<LoggedIn> {
return this.loggedIn$.asObservable();
}
クラスでログイン:
export interface ILoggedIn {
email: string;
password: string;
error: string;
}
export class LoggedIn implements ILoggedIn {
email: string;
password: string;
error: string
constructor(email: string, password: string, error: string) {
this.email = email;
this.password = password;
this.error = error;
}
}
これにより、ユーザーは観察可能なものを購読するだけで、あなたのアプリのどこにでもログインしたという通知を受けることができます:
constructor(
private ls: LoginService,
private ss: SettingsService) {
}
ngOnInit() {
this.ls.getLoggedIn()
.subscribe(
loggedIn => {
this.items = this.as.getLoggedInMenu();
var us = this.ss.getUserSettings();
this.ts.setTheme(us.theme);
});
}
あなたはドキュメントを参照することもできます。https://angular.io/docs/ts/latest/cookbook/component-communication.html – silentsod
を使用すると、親の親のためのリンクを持っていますか?私は子供に親が必要ない。 – AngularM
親子と同じ概念。コンポーネント間の通信は常に 'サービス 'と考える – PierreDuc