擴充診斷

函式應該在事件繫結中被呼叫

這項診斷會偵測事件繫結中未被呼叫的函式。

import {Component, signal, Signal} from '@angular/core';

@Component({
  template: `<button (click)="onClick">Click me</button>`,
})
class MyComponent {
  onClick() {
    console.log('clicked');
  }
}

這樣寫有什麼問題?

事件繫結中的函式應該在事件觸發時被呼叫。 如果函式沒有被呼叫,事件觸發時它就不會執行。

我應該怎麼做?

在事件繫結中使用函式時,請確認有呼叫它,讓函式能在事件觸發時執行。

import {Component} from '@angular/core';

@Component({
  template: `<button (click)="onClick()">Click me</button>`,
})
class MyComponent {
  onClick() {
    console.log('clicked');
  }
}

設定需求

任何擴充診斷要發出訊息,都必須啟用 strictTemplates。 uninvokedFunctionInEventBinding 除了 strictTemplates 之外,沒有其他額外需求。

如果無法避免,該怎麼辦?

可以透過編輯專案的 tsconfig.json 檔案來停用這項診斷:

{
  "angularCompilerOptions": {
    "extendedDiagnostics": {
      "checks": {
        "uninvokedFunctionInEventBinding": "suppress"
      }
    }
  }
}

詳情請見擴充診斷設定。