我使用的是角V.4.2.4。当我试图从API获取数据时,我得到了一个似乎找不到的错误。API在我自己的机器上,并且正在以邮递员的方式返回数据。我得到的错误是:
Can't resolve all parameters for DataService
以下是我认为有助于发现错误的文件:
应用程序模块
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { ProductComponent } from './shop/product.component';
import { DataService } from './shared/dataService';
@NgModule({
declarations: [
AppComponent,
ProductComponent
],
imports: [
BrowserModule,
HttpClientModule
],
providers: [DataService],
bootstrap: [AppComponent]
})
export class AppModule { }
数据服务
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map';
import { Product } from './product';
Injectable()
export class DataService {
constructor(private http: HttpClient) { }
public products: Product[] = [];
loadProducts(): Observable<boolean>{
return this.http.get('/api/products')
.map((data: any[]) => {
this.products = data;
return true;
})
}
}
产品.组件
import { Component, OnInit } from "@angular/core";
import { DataService } from '../shared/dataService';
import { Product } from '../shared/product';
@Component({
selector: "product-list",
templateUrl: "productList.component.html",
styleUrls: []
})
export class ProductComponent implements OnInit {
constructor(private data: DataService) { }
public products: Product[] = [];
ngOnInit(): void {
this.data.loadProducts()
.subscribe(success => {
if (success) {
this.products = this.data.products;
}
});
}
}
告诉我你还想看什么。谢谢你的帮助。