2

I am building an Angular library to use in a bigger project. It's going to be a multi-repo approach, with each of the parts of the app being a library. All of that parts are going to have their components, pipes, services...

But my first tests are not working. I created my standalone library and a hello-world component, built it with --watch, linked it with NPM to my base project... and it works. It shows the message on the screen. The problem comes when I try to add a pipe of any kind, even default pipes break. If create a Date object and use the date pipe after the message, it breaks. Same with currency or async pipes.

Error for date pipe:

Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[DatePipe -> InjectionToken LocaleId]: 
  StaticInjectorError(Platform: core)[DatePipe -> InjectionToken LocaleId]: 
    NullInjectorError: No provider for InjectionToken LocaleId!
Error: StaticInjectorError(AppModule)[DatePipe -> InjectionToken LocaleId]: 
  StaticInjectorError(Platform: core)[DatePipe -> InjectionToken LocaleId]: 
    NullInjectorError: No provider for InjectionToken LocaleId!
...

Error for async pipe:

Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[AsyncPipe -> ChangeDetectorRef]: 
  StaticInjectorError(Platform: core)[AsyncPipe -> ChangeDetectorRef]: 
    NullInjectorError: No provider for ChangeDetectorRef!
Error: StaticInjectorError(AppModule)[AsyncPipe -> ChangeDetectorRef]: 
  StaticInjectorError(Platform: core)[AsyncPipe -> ChangeDetectorRef]: 
    NullInjectorError: No provider for ChangeDetectorRef!
    at NullInjector.push.../../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:8896)
...

The library module looks like this:

@NgModule({
  declarations: [HolaMundoTestComponent],
  imports: [
    CommonModule
  ],
  exports: [HolaMundoTestComponent]
})
export class HolaMundoTestModule { }

And the component it exports:

@Component({
  selector: 'lib-hola-mundo-test',
  // templateUrl: './hola-mundo-test.component.html',
  // template: `<h1>Hola mundo en fecha {{ dateFrom | date }}</h1>`,
  template: `<h1>Hola mundo en fecha {{ text | async }}</h1>`,
  styleUrls: ['./hola-mundo-test.component.scss']
})
export class HolaMundoTestComponent {
  public text = new BehaviorSubject<string>('MITEXTO');
  public dateFrom = new Date();
  public money = 123245.234555;
  // constructor(public holaMundoTest: HolaMundoTestService) {}
}

Any ideas? I'm importing the CommonModule that holds all those default pipes and it's the tiniest library possible.

Flair
  • 2,609
  • 1
  • 29
  • 41
Sergio Tx
  • 3,706
  • 1
  • 16
  • 29

3 Answers3

3

Looks like a similar problem. Try to enable preserveSymlinks option in your angular.json file:

"architect": {
  "build": {
    "builder": "@angular-devkit/build-angular:browser",
    "options": {
      "preserveSymlinks": true,
Valeriy Katkov
  • 33,616
  • 20
  • 100
  • 123
0

In your library you have to export in a module all of your components and pipes. Then import that module wherever it is needed. This link should exemplify https://angular.io/guide/sharing-ngmodules

If that doesn't help, perhaps this library might be of use https://github.com/ng-packagr/ng-packagr. It is used to package angular libraries.

0

In your module import browser module and declare it to imports array:

import { BrowserModule } from '@angular/platform-browser';
@NgModule({
  declarations: [
    WeatherComponent
  ],
  imports: [
    HttpClientModule,
    BrowserModule
  ]
Timothy G.
  • 6,335
  • 7
  • 30
  • 46