Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: _this.flashMessagesService.show is not a function

Tags:

angular5

Trying to use angular2-flash-messages and getting the console error _this.flashMessagesService.show is not a function

Followed steps per https://github.com/moff/angular2-flash-messages

app.modules.ts

import { FlashMessagesModule } from 'angular2-flash-messages';

imports: [
    BrowserModule,
    FormsModule,
    FlashMessagesModule.forRoot(),
    RouterModule.forRoot(appRoutes)

signin.component.ts has

import { FlashMessagesService } from 'angular2-flash-messages';

signin.component.ts constructor has:

constructor(
private authService: AuthService,
private router: Router,
private flashMessagesService: FlashMessagesService
) { }

onSubmit() {
this.authService.signin(this.email, this.password)
.then((res) => {
this.flashMessagesService.show('You are signed in', {
cssClass: 'alert-success', timeout: 4000
});
this.router.navigate(['/']);
})
.catch((err) => {
this.flashMessagesService.show(err.message, {
cssClass: 'alert-danger', timeout: 4000
});
this.router.navigate(['/signin']);
});
}

and get the error _this.flashMessagesService.show is not a function

like image 426
Jim Bistis Avatar asked Feb 06 '26 23:02

Jim Bistis


1 Answers

   // put it inside of onSubmit() method
   var flashMessagesService = this.flashMessagesService;

  // and call it inside your .then function
  // Success
    flashMessagesService.show('You are signed in', {
       cssClass: 'alert-success', timeout: 4000
   });
  // Error
   flashMessagesService.show(err.message, {
      cssClass: 'alert-danger', timeout: 4000
   });

Here you can check https://github.com/moff/angular2-flash-messages/issues/35

like image 51
Nirav Joshi Avatar answered Feb 12 '26 13:02

Nirav Joshi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!