angular – 泛型类型’Observable’需要1个类型参数

前端之家收集整理的这篇文章主要介绍了angular – 泛型类型’Observable’需要1个类型参数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在Angular 2(TypeScript)代码下面给出了3以下的错误,如何解决它们.请建议.
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { NgModule,Component } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule,Http } from '@angular/http';
import 'rxjs/add/operator/map';
import { Observable } from "rxjs/Observable";

@Component({
    selector: 'http-client',template: `<h1>All Products</h1>
  <ul>
    <li *ngFor="let product of products">
       {{product.title}}
    </li>
  </ul>
  `})
class AppComponent {

    products: Array<string> = [];

    theDataSource: Observable;

    constructor(private http: Http) {

        this.theDataSource = this.http.get('api/products/')
            .map(res => res.json());
    }

    ngOnInit() {
        // Get the data from the server
        this.theDataSource.subscribe(
            data => {
                if (Array.isArray(data)) {
                    this.products = data;
                } else {
                    this.products.push(data);
                }
            },err =>
                console.log("Can't get products. Error code: %s,URL: %s ",err.status,err.url),() => console.log('Product(s) are retrieved')
        );
    }
}

@NgModule({
    imports: [BrowserModule,HttpModule],declarations: [AppComponent],bootstrap: [AppComponent]
})
class AppModule { }

platformBrowserDynamic().bootstrapModule(AppModule);

错误是,

> TS2314泛型类型’Observable’需要1个类型的参数.
> TS7006参数’data’隐式具有’any’类型.
> TS7006参数’err’隐式具有’any’类型.

theDataSource: Observable<any>;

任何可以(或应该如果可能的话)是一个更具体的类型,它匹配它应该发出的值的类型.

猜你在找的Angularjs相关文章