【问题标题】:Rxjs ~6.4.0 in Angular 8. Problem with mapRxjs ~6.4.0 in Angular 8. 地图问题
【发布时间】:2020-01-27 05:35:05
【问题描述】:

我将我的应用程序更新到 Angular 8 并且有新版本的 rxjs ~6.4.0。现在我的 .map 功能不起作用,我在 VC 中有一个错误

“可观察”类型上不存在属性“地图”。

我尝试通过多种方式导入地图

import 'rxjs/add/operator/map';

或者这样

import { map } from 'rxjs/operators';

结果一样。 代码如下

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
const email = this.userInfo.email;
return this.routApi.checkUser(email).map(e => {
  this.tradeOperService.writeOperRoomFromResponse(e);
  return true;
}).catch((e) => {
  if (e.error === 'traderoom') {
    this.router.navigate(['registration']);
  }
  if (e.status === 500) {
    this.authService.logout().subscribe(
      suc => {
        this.tokenService.deleteToken();
        this.tradeOperService.deleteOperRoomInfo();
        this.router.navigate(['login']);
      },
      err => {
        this.OperRoomService.deleteOperRoomInfo();
        this.tokenService.deleteToken();
      }
    );
  }
  return Observable.of(false);
});

这里的错误

return this.routApi.checkUser(email).map(e => {

与代码末尾的“of”相同的情况

return Observable.of(false);

【问题讨论】:

  • 根据这个站点:update.angular.io/#5.0:8.0,您可以尝试以下两个命令来尝试自动转换您的 RxJS 代码:npm (1) install -g rxjs-tslint (2) rxjs-5-to-6-migrate -p src/tsconfig.app.json
  • 新版本需要进行大量更改 (1) 现在所有导入都来自 rxjs 或 rxjs/operators (2) 管道方法需要使用任何运算符 (3) Observable.of 是现在只是 of (4) catch 现在是 catchError

标签: angular rxjs


【解决方案1】:

您现在需要使用管道链接运算符。改变这个:

return this.routApi.checkUser(email).map(e => {

到

return this.routApi.checkUser(email).pipe(
   map(e => {
   })
)

【讨论】:

    【解决方案2】:

    您正在正确使用/导入它 import { map } from 'rxjs/operators'

    您能否提供有关实施的详细信息

    检查用户(电子邮件)

    方法?

    如果它返回一个promise,使用from()从它变成Observable。

    导入“of”的正确方法是

    import { of } from 'rxjs';
    

    【讨论】:

    • checkUser(email: string): Observable { return this.http.get('api/user/check-user/' + email); }
    • 需要安装rxjs-compat
    【解决方案3】:

    需要安装rxjs-compat

    【讨论】:

    • Rxjs compat 不是解决方案,这是一种解决方法,它会增加你的包大小并让你在你的应用程序中编写过时的、不推荐使用的代码。你应该使用版本 6 的语法来做正确的事情。
    猜你喜欢
    • 2020-04-19
    • 1970-01-01
    • 2019-07-09
    • 2018-11-02
    • 1970-01-01
    • 2017-06-27
    • 2019-02-10
    • 1970-01-01
    • 2017-10-18
    相关资源
    最近更新 更多