ESLint 异步管道不应被否定

nop*_*nop 12 typescript eslint angular

我将 ESLint 与 Angular 一起使用,我不喜欢使用额外的代码,而(observable | async) === (false | null | undefined)不是仅仅使用(observable | async). 我如何禁用该规则?

E:\GitHub\skybot\angular\src\app\custom-layout\custom-layout.component.html
  6:75  error  Async pipes should not be negated. Use (observable | async) === (false | null | undefined) to check its value instead  @angular-eslint/template/no-negated-async
Run Code Online (Sandbox Code Playgroud)

自定义布局.component.html

<ng-template #sidenavRef>
  <vex-sidenav [collapsed]="sidenavCollapsed$ | async"></vex-sidenav>
</ng-template>

<ng-template #toolbarRef>
  <vex-toolbar [hasShadow]="toolbarShadowEnabled$ | async" [mobileQuery]="!(isDesktop$ | async)" class="vex-toolbar">
  </vex-toolbar>
</ng-template>

<ng-template #footerRef>
  <vex-footer *ngIf="isFooterVisible$ | async" class="vex-footer"></vex-footer>
</ng-template>

<ng-template #quickPanelRef>
  <vex-quick-panel></vex-quick-panel>
</ng-template>

<vex-layout [footerRef]="footerRef" [quickPanelRef]="quickPanelRef" [sidenavRef]="sidenavRef" [toolbarRef]="toolbarRef">
</vex-layout>

<vex-config-panel-toggle (openConfig)="configPanel.open()"></vex-config-panel-toggle>

<!-- CONFIGPANEL -->
<vex-sidebar #configPanel [invisibleBackdrop]="true" position="right">
  <vex-config-panel></vex-config-panel>
</vex-sidebar>
<!-- END CONFIGPANEL -->

Run Code Online (Sandbox Code Playgroud)

.eslintrc.json

{
  "root": true,
  "ignorePatterns": [
    "projects/**/*"
  ],
  "overrides": [
    {
      "files": [
        "*.ts"
      ],
      "parserOptions": {
        "project": [
          "tsconfig.json",
          "e2e/tsconfig.json"
        ],
        "createDefaultProgram": true
      },
      "extends": [
        "plugin:@angular-eslint/ng-cli-compat",
        "plugin:@angular-eslint/ng-cli-compat--formatting-add-on",
        "plugin:@angular-eslint/template/process-inline-templates"
      ],
      "rules": {
        "@angular-eslint/component-selector": [
          "error",
          {
            "type": "element",
            "prefix": "vex",
            "style": "kebab-case"
          }
        ],
        "@angular-eslint/directive-selector": [
          "error",
          {
            "type": "attribute",
            "prefix": "vex",
            "style": "camelCase"
          }
        ],
        "@angular-eslint/no-host-metadata-property": "off",
        "@typescript-eslint/explicit-member-accessibility": [
          "off",
          {
            "accessibility": "explicit"
          }
        ],
        "arrow-parens": [
          "off",
          "always"
        ],
        "id-blacklist": "error",
        "import/order": "off",
        "max-len": "off",
        "@angular-eslint/template/no-negated-async": "off"
      }
    },
    {
      "files": [
        "*.html"
      ],
      "extends": [
        "plugin:@angular-eslint/template/recommended"
      ],
      "rules": {}
    }
  ]
}


Run Code Online (Sandbox Code Playgroud)

Sam*_*ath 31

我认为这是一个非常好的规则,我们必须遵守。

\n

为什么?

\n
\n

Angular\xe2\x80\x99s 异步管道最初会在 observable 发出任何值或 Promise 解析之前发出 null。这可能会导致 \n 否定,例如 *ngIf=\xe2\x80\x9d!(myConditional | async)\xe2\x80\x9d 破坏布局\n并导致昂贵的副作用,例如关闭组件的 XHR 请求,\n不显示。

\n
\n

文档: http: //codelyzer.com/rules/templates-no-neated-async/

\n

所以你可以根据你的用例这样使用。即对我来说这只是一张boolean支票。

\n

(isMemberChanged$ | async) !== true

\n

  • 另一个解决方案是创建一个`| isTruthy` 管道将值强制为真/假。 (2认同)

小智 7

添加"@angular-eslint/template/no-negated-async": "off"到 esLint 规则部分的 html 部分