小编Boe*_*rne的帖子

"this"不能用于打字稿功能(Angular)

我想在Angular项目的typescript类中引用关键字"this".但它无法使用.我总是得到我想要改变的变量没有定义的错误.这是我的实现:

export class ContactComponent implements OnInit {
  contactForm: FormGroup;
  errorMsg:string = '';
  redirect = "";

  loggedIn(): void {
         this.redirect = "dashboard";
         console.log("success");
Run Code Online (Sandbox Code Playgroud)

在我的HTML中,重定向变量连接到routerLink,如下所示:

<a [routerLink]="redirect"></a>
Run Code Online (Sandbox Code Playgroud)

我已尝试使用其他函数中的其他变量,但始终存在相同的错误.

编辑:

loggedIn函数在另一个函数中作为"success"参数调用,如下所示:

submitForm(): void {
    DBEventProxy.instance().dbevent.login(this.contactForm['username'], 
    this.contactForm['password'], this.loggedIn, this.failed);
  }
Run Code Online (Sandbox Code Playgroud)

登录功能需要参数用户名,密码,成功功能,失败功能.

this typescript angular2-routing angular

2
推荐指数
1
解决办法
163
查看次数

标签 统计

angular ×1

angular2-routing ×1

this ×1

typescript ×1