【问题标题】:Angular - error TS2554: Expected 1 arguments, but got 0Angular - 错误 TS2554:预期 1 个参数,但得到 0
【发布时间】:2021-08-22 06:40:01
【问题描述】:

我正在使用 Angular-11。

我有这个代码:

header.component.html:

退出

header.component.ts:

logout($event: MouseEvent){
  // tslint:disable-next-line:no-unused-expression
  $event.preventDefault();
  this.loggedIn = false;
  this.token.remove();
  this.auth.changeAuthStatus(false);
  this.router.navigateByUrl('/login');
  this.notify.info("Logout Succesfully", {timeout:2000});
}

我收到了这个错误:

错误 TS2554:预期 1 个参数,但得到 0

退出

它突出显示 logout()。

我该如何解决这个问题?

谢谢

【问题讨论】:

  • 您的函数需要一个参数:logout($event: MouseEvent)。在模板 html 中使用时不传递任何参数。

标签: angular


【解决方案1】:

你的logout 函数有MouseEvent 参数,你没有在你的html文件中发送它。

你的 html 模板应该是

<a class="btn btn-danger btn-flat float-right" href="javascript:void(0)" (click)='logout($event)'>Sign out

【讨论】:

    【解决方案2】:

    你应该传递$event对象。

    (click)='logout($event)'
    

    【讨论】:

      【解决方案3】:

      您的 logout 方法需要一个您未提供的参数(事件)。

      在模板中你必须写(click)='logout($event)'

      $event 是 Angular 提供的代表事件的变量。

      【讨论】:

        猜你喜欢
        • 2021-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-19
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多