我有一个试图注入httpClient
的customerService
。错误发生在我注释//error happens on this line
的行上。在我尝试将httpClient注入我的服务之前,一切都会正常工作。
错误消息为:
`compiler.js:485 Uncaught Error: Can't resolve all parameters for CustomerService: (?).
at syntaxError (compiler.js:485)
at CompileMetadataResolver._getDependenciesMetadata (compiler.js:15700)
at CompileMetadataResolver._getTypeMetadata (compiler.js:15535)
at CompileMetadataResolver._getInjectableMetadata (compiler.js:15515)
at CompileMetadataResolver.getProviderMetadata (compiler.js:15875)
at compiler.js:15786
at Array.forEach (<anonymous>)
at CompileMetadataResolver._getProvidersMetadata (compiler.js:15746)
at CompileMetadataResolver.getNgModuleMetadata (compiler.js:15314)
at CompileMetadataResolver.getNgModuleSummary
(compiler.js:15133)`
不确定从哪里开始调试。任何帮助都将不胜感激。谢谢!
import { Observable } from "rxjs";
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
@Injectable()
export class CustomerService {
private baseUri: string = "hosthere.com"
constructor(
private http: HttpClient //error happens on this line
) { }
get(email: string): Observable<any> {
const uri = `${this.baseUri}/customer/${email}`;
return this.http.get(uri).map(res => res.json());
}
}
这是我的客户模块
import { NgModule } from "@angular/core";
import { SharedModule } from "../../shared/shared.module";
import { CustomerRouteModule } from "./cutomer.route.module";
import { SearchComponent } from "./";
import { CustomerService } from "../../services";
@NgModule({
imports: [SharedModule, CustomerRouteModule],
declarations: [SearchComponent],
providers: [CustomerService],
exports: []
})
export class CustomerModule {
}
这是我的共享模块
import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { FormsModule, ReactiveFormsModule } from "@angular/forms";
import { HttpClientModule } from "@angular/common/http";
import { AppMaterialModule } from "./app.material.module";
import { FlexLayoutModule } from "@angular/flex-layout";
import { HttpClientProvider } from "../services";
@NgModule({
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule,
HttpClientModule ,
AppMaterialModule
],
providers: [
],
exports: [
AppMaterialModule,
FlexLayoutModule,
FormsModule,
HttpClientModule
]
})
export class SharedModule {
}
发布于 2018-02-26 12:06:05
我在这里找到了答案:
我删除了我的多边形填充的反射,我真傻。
感谢大家的帮助。
发布于 2018-02-26 11:08:58
您可能遵循了http的旧指南/教程。
在使用HttpClient
时,您需要提供您想要返回的类型,实际上,您不需要对数据执行.map
操作,这会使您的代码变得更短。
get(email: string): Observable<any> {
const uri = `${this.baseUri}/customer/${email}`;
//add your model here something like
//return this.http.get<Customer>(uri);
return this.http.get<any>(uri);
}
请注意,如果提供响应类型,则不需要提供类型。以下是摘自官方doc的代码片段
getTextFile(filename: string) {
// The Observable returned by get() is of type Observable<string>
// because a text response was specified.
// There's no need to pass a <string> type parameter to get().
return this.http.get(filename, {responseType: 'text'})
.pipe(
tap( // Log the result or error
data => this.log(filename, data),
error => this.logError(filename, error)
)
);
}
https://stackoverflow.com/questions/48980818
复制相似问题