从Angular 2中的另一个模块导出服务

时间:2016-12-07 05:22:36

标签: angular

我正在尝试在单独的模块中创建可重用的服务,就像Angular 2's styleguide一样。但是当我尝试在另一个服务中使用该服务时出现错误:

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

这是我的核心模块:

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

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) { }
}

有什么想法吗?

感谢。

2 个答案:

答案 0 :(得分:3)

我认为您收到错误是因为您在@之前忘了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) { }
}

答案 1 :(得分:1)

对于其他发现此问题的人。在您的模块中,您的服务可能会在导出中列出。它应该在您的提供程序中;不在进口,声明或出口中。