【问题标题】:Type X is missing the following properties from type 'Observable<X>': _isScalar, source, operator, lift, and 6 more类型 X 缺少类型 'Observable<X>' 中的以下属性:_isScalar、source、operator、lift 和另外 6 个
【发布时间】:2020-03-16 12:25:08
【问题描述】:

我在 PostService 中向服务器请求少量数据:

getPosts(): Observable<Response> {
    return this.http.get<Response>(this._url, { headers: this.headers })
      .pipe(catchError(this.errorHandler));
}

然后我在 CategoryPageComponent 中调用这个 get 函数:

export class CategoryPageComponent implements OnInit {

  data: Observable<Response>;
  public posts= [];
  status;
  errorMsg;
  isLoaded: boolean = false

  constructor(private _navbarService: PostService, ) { }

  ngOnInit(): void {
    this.getCategoryPosts();
  }

  getCategoryPosts() {
    this._navbarService.getPosts().subscribe(
      data => {
        if (!data) {
          console.log(Error)
        } else {

          //Error here
          this.data = data;

          console.log(data)
          this.posts = data.result.BlogPost
          console.log(this.posts)
        }
      }) 
  }
}

我想绑定加载的数据:

<div *ngIf="data | async">
      <h4>{{posts[0].Title}}</h4>
</div>

但是,我的组件中的以下分配出现以下错误

this.data = data;
 Type 'Response' is missing the following properties from type 'Observable<Response>': _isScalar, source, operator, lift, and 6 more.

【问题讨论】:

    标签: angular asynchronous observable


    【解决方案1】:

    您应该分配this.data = this._navbarService.getPosts(),而不是分配接收到的数据。

    【讨论】:

      猜你喜欢
      • 2020-03-25
      • 1970-01-01
      • 2021-04-02
      • 2019-04-28
      • 2016-11-22
      • 1970-01-01
      • 1970-01-01
      • 2018-03-03
      • 1970-01-01
      相关资源
      最近更新 更多