1

I am applying toasterjs in my angular component. Here is my code. i have imported the ToastrService using the code:

import { ToastrService } from 'ngx-toastr';

and inject that service in the constructor

constructor(private toastrService: ToastrService) {    
}

this.toastrService.success('You are awesome!', 'Success!',)

But it is giving the following error:

Can't resolve all parameters for ToastrService: (?, [object Object], [object Object], [object Object], [object Object])

When I go to its definition, then it is taking 4 parameters. Here are the parameters:

success(message?: string, title?: string, override?: Partial<IndividualConfig>): ActiveToast;

I have added the module also:

imports: [
    ToastrModule.forRoot(),
    CommonModule,
    HttpModule,
    FormsModule,
    ReactiveFormsModule,
    TextboxModuleShared,
    DateTimeModuleShared,
    DropdownModuleShared
],

So how can I use the parameters because I want to only show success message?

David Walschots
  • 12,279
  • 5
  • 36
  • 59
Gaurav_0093
  • 1,040
  • 6
  • 28
  • 56

3 Answers3

1

You have imported the ToastrModule in your app's NgModule in the incorrect order. It should be imported in the order as indicated by the documentation:

@NgModule({
  imports: [
    CommonModule,
    BrowserAnimationsModule, // required animations module
    ToastrModule.forRoot(), // ToastrModule added
  ],
  bootstrap: [App],
  declarations: [App],
})
class MainModule {}

You should also replace CommonModule with BrowserModule if this app is running in the browser as indicated here.

Note that the error doesn't occur while calling the success function, but while invoking the constructor of the ToastrService. Your error message clearly states that not all parameters for the ToastrService constructor can be resolved. Some of these parameters cannot be resolved, because no instances for those parameters have been created yet. To understand how this works, you should read the documentation on dependency injection within the Angular framework.

David Walschots
  • 12,279
  • 5
  • 36
  • 59
0

Point to Consider : I was facing same issue. So here how I get solution

  • After installing 'ngx-toastr' Package and updating angular.json file with 'toastr.css

  • angular should be re-run by using ng-serve command, because app is not rendered by changing angular.json file

mabdullahse
  • 3,474
  • 25
  • 23
0

Try to import 'Inject' decorator and then decorate parameter in constructor and as an argument you should add ToastrService like this:

constructor(@Inject(ToastrService) private toastyService: ToastrService ) { }

Nedzad G
  • 1,007
  • 1
  • 10
  • 21