Angular&Eslint-this.router.navigate(['/'])没有浮动承诺错误
我有以下 eslint 错误:
必须正确处理 Promise 或使用
voidoperator.eslint@typescript-eslint/no-floating-promises明确标记为忽略
使用此代码:
this.router.navigate(['/']);
路由器是这样导入的:import { Router } from '@angular/router';并在类的构造函数中分配。
我的 eslint 配置:
module.exports = {
extends: [
"plugin:@angular-eslint/recommended",
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
// "plugin:@angular-eslint/template/process-inline-templates",
'prettier',
],
parser: '@typescript-eslint/parser',
parserOptions: {
project: './tsconfig.eslint.json',
sourceType: 'module',
ecmaVersion: 2020,
},
plugins: ['@typescript-eslint', '@angular-eslint', 'prettier'],
rules: {
"prettier/prettier": [
"error",
{
"printWidth": 140,
"singleQuote": true,
"useTabs": false,
"tabWidth": 2,
"semi": true,
"bracketSpacing": true,
"endOfLine": "lf"
},
],
},
};
是我的配置有问题还是有什么问题?我觉得 router.navigate 不应该是角度项目中的 eslint 错误。
回答
#no-floating-promises规则说:
这条规则禁止在语句中使用类似 Promise 的值而不适当地处理它们的错误
所以在这里你没有this.router.navigate适当地处理,因为它返回一个Promise.
您可以:
-
等它回来
await this.router.navigate(['/']); -
明确地将其标记为有意未等待
void this.router.navigate(['/']); -
禁用此特定行的规则
// eslint-disable-next-line @typescript-eslint/no-floating-promises this.router.navigate(['/']);
THE END
二维码