2016-12-07 4 views
3

Angular 2's styleguideのように、別のモジュール内で再利用可能なサービスを作成しようとしています。私は別のサービス内でそのサービスを利用しようとするとエラーが発生しますが:角2の別のモジュールからサービスをエクスポート

import { NgModule, Optional, SkipSelf } from '@angular/core'; 
import { CommonModule } from '@angular/common'; 
import { throwIfAlreadyLoaded } from './module-import-guard'; 
import { HttpClientService } from './httpClient.service'; 
import { HttpModule } from '@angular/http'; 

@NgModule({ 
    imports: [ 
     CommonModule, 
     HttpModule 
    ], 
    exports: [], 
    declarations: [], 
    providers: [HttpClientService] 
}) 
export class CoreModule { 
    constructor(@Optional() @SkipSelf() parentModule: CoreModule) { 
     throwIfAlreadyLoaded(parentModule, 'CoreModule'); 
    } 
} 

マイHttpClientService

ここUncaught Error: Can't resolve all parameters for AttendanceSummaryService:

は私のコアモジュールである

import { Injectable } from '@angular/core'; 
import { Http, Headers } from '@angular/http'; 

@Injectable() 
export class HttpClientService { 

    constructor(private http: Http) { } 

    createAuthorizationHeader(headers: Headers) { 
     headers.append('Authorization', 'Bearer ' + ''); 
    } 

    get(url) { 
     let headers = new Headers(); 
     this.createAuthorizationHeader(headers); 
     return this.http.get(url, { 
      headers: headers 
     }); 
    } 

    post(url, data) { 
     let headers = new Headers(); 
     this.createAuthorizationHeader(headers); 
     return this.http.post(url, data, { 
      headers: headers 
     }); 
    } 
} 

そして、私のコアモジュールをインポートするapp.module:

import { NgModule, ApplicationRef } from '@angular/core'; 
import { BrowserModule } from '@angular/platform-browser'; 
import { HttpModule } from '@angular/http'; 
import { AppComponent } from './app.component'; 
import { AttendanceSummaryService } from './home/attendance-summary.service'; 
import { CoreModule } from './core/core.module'; 
import { HttpClientService } from './core/httpClient.service'; 

import {FormControl, FormGroup, ReactiveFormsModule} from '@angular/forms'; 


@NgModule({ 
    bootstrap: [AppComponent], 
    declarations: [ 
    AppComponent, 
    AttendanceSummaryComponent, 
    ], 
    imports: [ 
    BrowserModule, 
    FormsModule, 
    ReactiveFormsModule, 
    HttpModule, 
    CoreModule 
    ], 
    providers: [ 
    HttpClientService, 
    AttendanceSummaryService  

    ] 
}) 
export class AppModule { 
    constructor(public appRef: ApplicationRef) { } 
} 

そしてHttpClientServiceを消費サービス:

import { Injectable } from '@angular/core'; 
import { Observable } from 'rxjs/Rx'; 
import { AttendanceSummary } from './attendance-summary.model'; 
import { Http, Response, Headers, RequestOptions } from '@angular/http'; 
import { HttpClientService } from '../core/httpClient.service'; 


Injectable() 
export class AttendanceSummaryService { 

    private apiBaseUrl = '/api/v1/agency/attendances/'; 
    constructor(private http: HttpClientService) { } 
} 

任意のアイデア?

ありがとうございました。

答えて

0

私はあなたがInjectable@を忘れてしまったので、あなたがエラーを取得していると思う:

import { Injectable } from '@angular/core'; 
import { Observable } from 'rxjs/Rx'; 
import { AttendanceSummary } from './attendance-summary.model'; 
import { Http, Response, Headers, RequestOptions } from '@angular/http'; 
import { HttpClientService } from '../core/httpClient.service'; 

@Injectable() 
export class AttendanceSummaryService { 

    private apiBaseUrl = '/api/v1/agency/attendances/'; 
    constructor(private http: HttpClientService) { } 
} 
関連する問題