擴充診斷

函式應該在文字插值中被呼叫

這項診斷會偵測文字插值中未被呼叫的函式。

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

@Component({
  template: `{{ getValue }}`,
})
class MyComponent {
  getValue() {
    return 'value';
  }
}

這樣寫有什麼問題?

文字插值中的函式應該被呼叫,才能回傳一個值。 如果函式沒有被呼叫,它就不會回傳任何值,插值也不會如預期般運作。

我應該怎麼做?

在文字插值中使用函式時,請確認有呼叫它來回傳值。

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

@Component({
  template: `{{ getValue() }}`,
})
class MyComponent {
  getValue() {
    return 'value';
  }
}

設定需求

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

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

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

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

詳情請見擴充診斷設定。