【问题标题】:Converting function from RxJS v5.x to v6将函数从 RxJS v5.x 转换为 v6
【发布时间】:2018-09-06 12:51:45
【问题描述】:

我开始使用 Angular 5 模板并将其转换为 Ang 6。 https://github.com/mmacneil/AngularASPNETCore2WebApiAuth

我知道 RxJS 发生了变化,例如RxJS v5.x 到 v6。 那么我如何将此代码转换为 v6,因为 .map 已完全更改。

register(email: string, password: string, firstName: string, lastName: string,location: string, gender:string, birthDate:any): Observable<UserRegistration> {
let body = JSON.stringify({ email, password, firstName, lastName,location,gender,birthDate });
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });

return this.http.post(this.baseUrl + "/accounts", body, options)
  .map(res => true)
  .catch(this.handleError);
}

用户注册

export interface UserRegistration {
email: string;  
password: string;
firstName: string;
lastName:  string;
location: string;
birthDate: any;
gender: string;

}

【问题讨论】:

    标签: angular rxjs angular6


    【解决方案1】:
    return this.http.post(this.baseUrl + "/accounts", body, options).pipe(
      map(res => true),
      catchError(this.handleError)
    }
    

    值得知道的是,如果您现在不想将所有 RxJs 代码从 5 转换为 6,您可以安装 rxjs-compat,它将同时支持这两个版本(假设您也安装了 6)

    当您使用 ng update 将 ng5 升级到 ng6 时,您将自动安装 rxjs-compat。请参阅https://blog.angular.io/version-6-of-angular-now-available-cc56b0efa7a4#7a6c 了解更多信息。

    【讨论】:

    • 这对 .map 错误有效,但不幸的是它没有解决原始问题,我认为这可能不是 Ang v5 中的错误。错误构建:类型“Observable”不可分配给类型“Observable”。
    【解决方案2】:

    使用管道运算符。将catch 替换为catchError

    return this.http.post(this.baseUrl + "/accounts", body, options)
      .pipe(
         map(res => true),
         catchError(this.handleError)
       )
    }
    

    【讨论】:

      猜你喜欢
      • 2019-12-05
      • 2018-11-03
      • 1970-01-01
      • 2021-10-03
      • 2020-06-21
      • 2023-03-27
      • 2020-07-31
      • 2018-10-08
      • 2019-08-30
      相关资源
      最近更新 更多