如何在Typescript回调中编写/转换多行JavaScript代码?

如何在Typescript回调中编写/转换多行JavaScript代码?,javascript,typescript,Javascript,Typescript,我有一个回调(subscribe),它在ajax调用的响应完成时执行 this.workerService.isLogin() .subscribe(res => this.isLoginSuccess = res; console.log(this.isLoginSuccess); this.router.navigateByUrl('/employees'); ); 它是typescript,相当于: (function

我有一个回调(subscribe),它在ajax调用的响应完成时执行

this.workerService.isLogin()
  .subscribe(res => this.isLoginSuccess = res; 
             console.log(this.isLoginSuccess); 
             this.router.navigateByUrl('/employees');
  );
它是typescript,相当于:

(function (res) { return _this.isLoginSuccess = res; });
console.log(this.isLoginSuccess);
this.router.navigateByUrl('/employees');
我想要的是:

(function (res) { 

    return _this.isLoginSuccess = res; 
    console.log(this.isLoginSuccess);
    this.router.navigateByUrl('/employees');

});

我不熟悉Typescript。

您需要在那里创建一个块:

this.workerService.isLogin()
  .subscribe(res => {
      this.isLoginSuccess = res; 
      console.log(this.isLoginSuccess); 
      this.router.navigateByUrl('/employees');
  });

这真的应该抛出一个语法错误,而不是转换成任何东西。@Bergi:注意到了。学习::)还有一件事。我看到只有当res为true(从服务器返回)时才会执行该块。当res不是true时,我如何处理这种情况?这取决于
isLogin
何时发出事件的实现。