跳到正文
本页尚未翻译成中文,以下为英文原文。 查看英文版

Unused @let declaration ​

This diagnostic detects @let declarations whose value is never read in the template.

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

@Component({
  template: `
    @let fullName = user.firstName + ' ' + user.lastName;
    <h1>Welcome</h1>
  `,
})
class MyComponent {
  user = {firstName: 'Ada', lastName: 'Lovelace'};
}

What's wrong with that? ​

An unused @let is usually left over from a refactor, or points to a place where the value was meant to be used. Angular still evaluates the expression of an unused @let each time the template updates. For example, a function call in the expression still runs.

What should I do instead? ​

Either use the declaration or remove it:

ts
@Component({
  template: `
    @let fullName = user.firstName + ' ' + user.lastName;
    <h1>Welcome, {{ fullName }}</h1>
  `,
})
class MyComponent {
  user = {firstName: 'Ada', lastName: 'Lovelace'};
}
ts
@Component({
  template: `<h1>Welcome</h1>`,
})
class MyComponent {}

What if I can't avoid this? ​

This diagnostic can be disabled by editing the project's tsconfig.json file:

json
{
  "angularCompilerOptions": {
    "extendedDiagnostics": {
      "checks": {
        "unusedLetDeclaration": "suppress"
      }
    }
  }
}

See extended diagnostic configuration for more info.

基于 MIT 许可证发布。