Background:
ng generate library forms-lib
Question:
I am trying, without success, to create a library and make the services in it available for use in applications which need them.
This code does not work:
import { NgModule } from '@angular/core';
import { FormsLibComponent } from './forms-lib.component';
import { FormsLibService } from './forms-lib.service';
@NgModule({
imports: [],
declarations: [FormsLibComponent],
exports: [FormsLibComponent, FormsLibService],
})
export class FormsLibModule {
}
Returns error:
Uncaught Error: Can't export value FormsLibService from FormsLibModule
as it was neither declared nor imported!
Can someone point me in the right direction?
Thank you.
The Purpose of Angular Libraries To create a library, we generate it by “ng generate” command, built it by “ng build” command, publish by “npm publish” command. To use a library we install it by “ng i “ command.
In your module, you may have your service listed in exports. It should be in your providers; not in imports, declarations, or exports.
You can add services you want to export from your library by adding some code into public_api.ts
.
First locate projects/nameOfLibProject/src/public_api.ts
inside your angular app then add
export * from './lib/locationOf/your.service';
This might do the trick for you:
import { NgModule, ModuleWithProviders } from '@angular/core';
import { FormsLibComponent } from './forms-lib.component';
import { FormsLibService } from './forms-lib.service';
@NgModule({
declarations: [FormsLibComponent],
exports: [FormsLibComponent],
})
export class FormsLibModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: FormsLibModule,
providers: [FormsLibService]
};
}
}
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