Reputation: 1995
I would like to have an Angular 6 library that only publishes a singleton service.
Here is a simple Angular library that only contains this service:
test54-library/src/lib/test54-library.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class Test54LibraryService {
constructor() { }
testCall() {
console.log('this is a testcall');
}
}
test54-library/src/public_api.ts
export * from './lib/test54-library.service';
Do I have to have a module like this in this library that references this service in a providers entry? Why is a module needed in this usecase?
@NgModule()
export class Test54Module {
static forRoot():ModuleWithProviders {
return {
ngModuleTest54Module,
providers: [
Test54LibraryService
]
}
}
}
Upvotes: 1
Views: 1906
Reputation: 266
You don't need to provide the service again. the providedIn: 'root'
does the work for you.
Please refer: singleton-services for more information.
Upvotes: 1