通用类型"Observable T"需要1个类型的参数 [英] Generic type 'Observable<T>' requires 1 type argument
本文介绍了通用类型"Observable T"需要1个类型的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的Angular 2(TypeScript)代码给了我3以下的错误,如何解决它们.请提出建议.
Below Angular 2 (TypeScript) code gave me below 3 error, how to resolve them. please suggest.
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通用类型可观察"需要1个类型参数.
- TS7006参数'data'隐式具有'any'类型.
- TS7006参数'err'隐式具有'any'类型.
推荐答案
theDataSource: Observable<any>;
其中any
可以(或者应该尽可能)是更具体的类型,与应该发出的值的类型匹配.
where any
can (or should be if possible) be a more concrete type that matches the type of the values it is supposed to emit.
这篇关于通用类型"Observable T"需要1个类型的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文