angular 2 update app component from router outlet component

梦想与她 提交于 2019-12-05 10:26:41

Not sure what you mean by "and the data is passed".

@Output() loginSuccess = new EventEmitter<IAuthResponse>();

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.

@Injectable() 
export class LoginService {
  changes:BehaviorSubject = new BehaviorSubject(false);
}
export class LoginComponent {
  constructor(private loginService:LoginService) {}

  onLogin() {
    this.loginService.changes.next(true);
  }
}
export class NavComponent {
  constructor(private loginService:LoginService) {}

  onLogin() {
    this.loginService.changes.subscribe(status => this.isLoggedIn = status);
  }
}

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:

changes:BehaviorSubject = new BehaviorSubject(false);

gives an error saying BehaviorSubject needs a type declaration, so I tried both

changes:BehaviorSubject<boolean> = new BehaviorSubject(false);
    as well as
changes:BehaviorSubject<any> = new BehaviorSubject(false);

and it compiles fine, but when running the app, it gives a non-descriptive error of:

zone.js:323 SyntaxError: Unexpected token <
Evaluating http://localhost:3000/node_modules/rxjs/index.js
Error loading http://localhost:3000/app/main.js
at SystemJSLoader.__exec
(http://localhost:3000/node_modules/systemjs/dist/system.src.js:1395:16)
at entry.execute 

I'm assuming something has changed with rxjs and BehaviorSubject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!