Angular 无法注入 HttpClient,出现“无法解析 CustomerService 的所有参数”错误
Angular cant inject HttpClient getting 'Can't resolve all parameters for CustomerService' error
我有一个 customerService
,我正在尝试注入 httpClient
。我评论 //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 {
}
您可能关注了旧的 guide/tutorial 的 http。
使用 HttpClient
时,您需要提供想要 return 的类型,实际上,您不需要 .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)
)
);
}
我在这里找到了答案:
我从我的 polyfill 中删除了反射,愚蠢的我。
谢谢大家的帮助。
创建新文件或添加到现有文件 polyfills.ts
import 'core-js/es7/reflect';
包含到 webpack.config
module.exports = {
mode: 'production',
devtool: 'source-map',
entry: {
'app': './ClientApp/main.ts',
'polyfills': './ClientApp/polyfills.ts'
},...
对我有帮助
我有一个 customerService
,我正在尝试注入 httpClient
。我评论 //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 {
}
您可能关注了旧的 guide/tutorial 的 http。
使用 HttpClient
时,您需要提供想要 return 的类型,实际上,您不需要 .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)
)
);
}
我在这里找到了答案:
我从我的 polyfill 中删除了反射,愚蠢的我。
谢谢大家的帮助。
创建新文件或添加到现有文件 polyfills.ts
import 'core-js/es7/reflect';
包含到 webpack.config
module.exports = {
mode: 'production',
devtool: 'source-map',
entry: {
'app': './ClientApp/main.ts',
'polyfills': './ClientApp/polyfills.ts'
},...
对我有帮助