Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 645
frint Avatar asked Oct 24 '19 11:10

frint


1 Answers

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]
})
like image 198
Arcteezy Avatar answered Oct 29 '22 11:10

Arcteezy