I'm using angular 2 rc1 and the new router @angular/router
In app component I have a nav section and router outlet
<nav>
<a *ngIf="auth?.userName == null" [routerLink]="['/login']">Login</a>
<a *ngIf="auth?.userName != null" (click)="logout()">Logout</a>
{{auth?.userName}}
</nav>
<router-outlet></router-outlet>
I inject a loginService into app component and subscribe to an event in ngOnInit
ngOnInit() {
this.loginService.loginSuccess.subscribe(this.loginSuccess);
}
ngOnDestroy() {
this.loginService.loginSuccess.unsubscribe();
}
private loginSuccess(res: IAuthResponse) {
this.auth = res;
}
when I navigate to my '/login' route page/component my loginService is inject and loginService defines an event
@Output() loginSuccess = new EventEmitter<IAuthResponse>();
and after successful login in my service, the login service raises the event
this.loginSuccess.next(response);
I'm able to set a breakpoint in app component on the subscribed loginSucess and the data is passed along, however 'this' is undefined.
private loginSuccess(res: IAuthResponse) {
this.auth = res; //this is undefind
}
how can I update the app component from events that are triggerd from services used in components that are hosted in the router outlet
This looks like it could be the solution to my problem: passing events from child to parent that uses router-outlet, however it looks like RC1 has changed how BehaviorSubject works. Using the method above:
gives an error saying BehaviorSubject needs a type declaration, so I tried both
and it compiles fine, but when running the app, it gives a non-descriptive error of:
I'm assuming something has changed with rxjs and BehaviorSubject
Not sure what you mean by "and the data is passed".
in the component added by the router (
LoginComponent
) doesn't do anything.@Input()
and@Output()
in routed components are not supported.Just inject
LoginService
to the routed component and emit the event using an observable in the login component.