在角2中承诺调用事件发射器

问题描述:

我无法让我的事件发射器在承诺时的方法中正常运行。我没有收到任何错误。它只是根本不会调用在角2中承诺调用事件发射器

的子组件

,:

@Output() isLoggingIn = new EventEmitter<boolean>(); 

onLogin() { 
    this.isLoggingIn.emit(true); //<-- this gets called, so it works. 
    this.authService.loginUser(this.loginForm.value) 
    .then(() => { 
     console.log(this.isLoggingIn.emit); //<-- This logs out the method. 
     this.isLoggingIn.emit(false); //<-- This doesn't get called! 
    }) 
    .catch((err) => console.log(err)); 
} 

这是父组件的HTML模板,header.component.html:

<tas-loginform 
    (isLoggingIn)="setLoginLoading($event)" 
    *ngIf="!isSigningUp"> 
</tas-loginform> 

这是父组件打字稿代码,header.component.ts:

setLoginLoading(bool: boolean) { 
    console.log(bool); 
    this.loginLoading = bool; 
} 

这几乎就好像事件发射器在promi中调用因为它不会运行setLoginLoading的console.log,所以回调永远不会被调用。而且它更加困难,因为根本没有错误信息。

+0

你确定'.then()'处理程序中的'this'是否指向你想要的对象?我记得上次我在JS工作时,这是一个棘手的问题。 – Douglas

+0

我确定。我做了一个plunkr,试图复制这个问题。但是plunkr实际上工作,而我的代码没有。我猜可能还有其他的事情...... http://embed.plnkr.co/l50UlTSAh3p102lG5Db9/ – TheMarlow

+0

也许你加载区和polyfills的顺序? –

这个问题与路由如何搞乱我的身份验证状态有关。我真的无法解释它。我在我的项目中拿出了路由,并且不知何故,一切都很好。