【问题标题】:Angular, HttpClient; Property '.shareReplay' does not exist on type 'Observable'角,HttpClient; “可观察”类型上不存在属性“.shareReplay”
【发布时间】:2020-12-27 00:14:42
【问题描述】:

对不起,如果这是一个明显的问题。 我正在关注本教程:https://blog.angular-university.io/angular-jwt-authentication/

我像教程中一样创建了服务,但它告诉我“可观察”类型上不存在属性“.shareReplay”,我想我的导入不正确,但我似乎找不到正确的。

import {Injectable} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {User} from "./model/user";

@Injectable({
  providedIn: 'root'
})
export class AuthenticationService {

  constructor(private http: HttpClient) {
  }

  login(username: string, password: string){
    return this.http.post<User>('/login', {username, password})
      .shareReplay();
  }
}

【问题讨论】:

    标签: angular typescript angular-httpclient


    【解决方案1】:

    这在rxjs v5 之前是有效的,但是现在你需要像下面这样使用 pipable 操作符

    import {shareReplay } from 'rxjs/operators'
    
    
    
    login(username: string, password: string){
        return this.http.post<Body>('/login', {username, password}).pipe(
          shareReplay()
        )
          
      }
    

    【讨论】:

      猜你喜欢
      • 2017-04-01
      • 2017-02-27
      • 2018-11-03
      • 1970-01-01
      • 2018-12-05
      • 2019-03-09
      • 2018-01-28
      • 2018-10-12
      • 2016-09-25
      相关资源
      最近更新 更多