No Provider for Datepipe [Duplicate]

No Provider for Datepipe [Duplicate]

I tried to use DatePipe in my service like this:

import { Injectable } from '@angular/core';
import { DatePipe } from '@angular/common';
import { TranslateService } from '@ngx-translate/core';

@Injectable({
   providedIn: 'root',
})
export class BaseService {

    public items = [];
    constructor(
        protected datePipe: DatePipe,
        protected translateService: TranslateService
    ) { }
}

But I get this error: NullInjectorError: No provider for DatePipe!". What is the cause?

3

1 Answer

Just add the DatePipe in the NgModule providers section in your app.module.ts file.

import { DatePipe } from '@angular/common';

...

@NgModule({
  declarations: [...],
  imports: [...],
  providers: [
    DatePipe,
    ...
  ],
  bootstrap: [AppComponent]
})
David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.