当我打开缰绳时,上面写着:
subscribe is deprecated: Use an observer instead of an error callback
angular应用的代码:
this.userService.updateUser(data).pipe(
tap(() => {bla bla bla})
).subscribe(
this.handleUpdateResponse.bind(this),
this.handleError.bind(this)
);
我不知道该用什么,怎么用……
谢谢!
我把我的Angular项目从TSLint迁移到ESLint,现在它不再显示警告了!
我遵循这些步骤。(在每个步骤结束时,我还建议提交更改)
Add eslint:
ng add @angular-eslint/schematics
Convert tslint to eslint:
ng g @angular-eslint/schematics:convert-tslint-to-eslint
Remove tslint and codelyzer: npm uninstall -S tslint codelyzer
If you like to auto fix many of the Lint issues
ng lint --fix (It will also list the not fixed issues)
In VSCode uninstall the TSLint plugin, install ESLint plugin and Reload the VSCode.
Make sure it updated the package and package-lock files. Also the node_modules in your project.
If you have the tsconfig.json files under sub directory - you need to add/update the projects-root-directory/.vscode/settings.json with the sub directory where the tsconfig files are!
{
"eslint.workingDirectories": [
"sub-directory-where-tsconfig-files-are"
]
}
VS Code官方页面的信息:从TSLint迁移到ESLint(谢谢你在评论中指出这一点!)
Angular从TSLint迁移到ESLint Reference
如果你有一个对象类型为Observable<T> | Observable<T2> -而不是Observable<T|T2>,你就会得到这个错误。
例如:
const obs = (new Date().getTime() % 2 == 0) ? of(123) : of('ABC');
编译器不会将类型为Observable<number |的obs设为字符串>。
它可能会让你惊讶,下面会给你错误使用一个观察者而不是一个完整的回调和期望2-3个参数,但得到1。
obs.subscribe(value => {
});
这是因为它可以是两种不同类型中的一种,而编译器不够聪明,无法协调它们。
你需要改变你的代码返回Observable<number | string>而不是Observable<number> | Observable<string>。这其中的微妙之处取决于你在做什么。
详见官方网站
https://rxjs.dev/deprecations/subscribe-arguments
注意下面第二个订阅代码中的{}大括号。
import { of } from 'rxjs';
// recommended
of([1,2,3]).subscribe((v) => console.info(v));
// also recommended
of([1,2,3]).subscribe({
next: (v) => console.log(v),
error: (e) => console.error(e),
complete: () => console.info('complete')
})
我把我的Angular项目从TSLint迁移到ESLint,现在它不再显示警告了!
我遵循这些步骤。(在每个步骤结束时,我还建议提交更改)
Add eslint:
ng add @angular-eslint/schematics
Convert tslint to eslint:
ng g @angular-eslint/schematics:convert-tslint-to-eslint
Remove tslint and codelyzer: npm uninstall -S tslint codelyzer
If you like to auto fix many of the Lint issues
ng lint --fix (It will also list the not fixed issues)
In VSCode uninstall the TSLint plugin, install ESLint plugin and Reload the VSCode.
Make sure it updated the package and package-lock files. Also the node_modules in your project.
If you have the tsconfig.json files under sub directory - you need to add/update the projects-root-directory/.vscode/settings.json with the sub directory where the tsconfig files are!
{
"eslint.workingDirectories": [
"sub-directory-where-tsconfig-files-are"
]
}
VS Code官方页面的信息:从TSLint迁移到ESLint(谢谢你在评论中指出这一点!)
Angular从TSLint迁移到ESLint Reference