类型"Observable< IProduct []>"中缺少属性"includes" [英] Property 'includes' is missing in type 'Observable<IProduct[]>'

查看:79
本文介绍了类型"Observable< IProduct []>"中缺少属性"includes"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是打字稿新手,却出现此错误

I am new to typescript, and I get this error

类型可观察"不能分配给类型"IProduct []". 类型可观察"中缺少属性包含".

Type 'Observable' is not assignable to type 'IProduct[]'. Property 'includes' is missing in type 'Observable'.

我的服务类别是:

getProducts() : Observable<IProduct[]> {
    return this._http.get<IProduct[]>(this._productUrl)
    .do(data=>console.log('All: ' + JSON.stringify(data)))
    .catch(this.handleError);
}


private handleError(err : HttpErrorResponse){
    console.log(err.message);
    return Observable.throw(err.message)
}

哪里出问题了,我应该怎么做才能解决?

Where is it wrong and what should I do to fix it?

推荐答案

如果这个问题来自@DeborahKurata的Angular课程,由@DeborahKurata提供,则答案在下一个模块订阅可观察对象"中.

If this question comes from the Angular course on Pluralsight by @DeborahKurata, the answer is in the next module, "Subscribing to an Observable".

在product-list.component.ts中的ngOnInit(): void方法中,对此进行更改

In the ngOnInit(): void method in product-list.component.ts, change this

    this.products = this._productService.getProducts();
    this.filteredProducts = this.products;

    this._productService.getProducts()
        .subscribe(products => { 
            this.products = products; 
            this.filteredProducts = this.products; 
        }, error => this.errorMessage = <any>error);

这篇关于类型"Observable&lt; IProduct []&gt;"中缺少属性"includes"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆