angular - In angular2 I want my component to talk to another component, how is this possilbe? -
in angular2 want component talk component, how possilbe?
i have component needs broadcast component that component can update itself.
i know how in angular v1.
i have no code show, have no idea how this.
the components separate , not nested. how can them talking each other e.g. event or something?
i have login service defines loggedin observable:
private loggedin$: subject<loggedin>; constructor() { this.loggedin$ = <subject<loggedin>>new subject(); }
i defined 2 methods set , get:
setloggedin(loggedin: loggedin): void { this.loggedin$.next(loggedin); } getloggedin(): observable<loggedin> { return this.loggedin$.asobservable(); }
the logged in class:
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; } }
with that, can notified user logged in anywhere in app subscribing observable:
constructor( private ls: loginservice, private ss: settingsservice) { } ngoninit() { this.ls.getloggedin() .subscribe( loggedin => { this.items = this.as.getloggedinmenu(); var = this.ss.getusersettings(); this.ts.settheme(us.theme); }); }
Comments
Post a Comment