Angular cant注入HttpClient,无法解析CustomerService'的所有参数。错误

时间:2018-02-26 02:55:22

标签: typescript angular2-routing injectable

我有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 {

}

2 个答案:

答案 0 :(得分:1)

您可能会关注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)
      )
    );
}

答案 1 :(得分:0)

创建新文件或添加到存在的 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'
    },...

对我有帮助