I'm trying to create a reusable service within a separate module, much like Angular 2's styleguide. However I get an error when I trying to use that service within another service:
Uncaught Error: Can't resolve all parameters for AttendanceSummaryService:
Here is my core module:
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');
}
}
My 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
});
}
}
And my app.module which imports the core 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) { }
}
And the service which consumes the 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) { }
}
Any ideas?
Thanks.
Services provided in @NgModule() are shared with the whole application and therefore also with all modules in your application (except when the module where you provide the service is lazy loaded). If you've provided a service in an @NgModule, all non-lazy-loaded modules below may use that service.
ES modules can be imported in one of two ways: either statically, using the import ... from '...' syntax, or dynamically, using the import() method. Inside of a service worker, only the static syntax is currently supported.
An export what you put is the exports property of the @NgModule decorator. It enables an Angular module to expose some of its components/directives/pipes to the other modules in the applications. Without it, the components/directives/pipes defined in a module could only be used in that module.
There are several ways how Angular components can pass data around: Using @Input and @Output. By injecting parent component through constructor or child components through @ViewChild , @ViewChildren , @ContentChild , @ContentChildren and directly calling component's API.
For others finding this question. In your module, you may have your service listed in exports. It should be in your providers; not in imports, declarations, or exports.
@NgModule({
imports: [],
exports: [],
declarations: [],
providers: [export-service-here]
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With