this.flashMessage.show不是函数

时间:2018-01-07 17:09:08

标签: angular

我在我的应用程序中使用angular2-flash-messages进行通知。成功注册后,我必须显示用户已注册。但我正面临一个错误,即#34;

  

this.flashMessage.show不是函数

"

signup.component.ts:

import { Component, OnInit } from '@angular/core';
import { FlashMessagesService } from 'angular2-flash-messages';
import { AuthService } from '../services/auth.service';
import { Router } from '@angular/router';

@Component({
  selector: 'app-signup',
  templateUrl: './signup.component.html',
  styleUrls: ['./signup.component.css']
})
export class SignupComponent implements OnInit {

  name: String;
  username: String;
  email: String;
  password;

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

  ngOnInit() {
  }
  onRegisterSubmit() {
    const user = {
      name: this.name,
      email: this.email,
      username: this.username,
      password: this.password
    };

    this.authService.registerUser(user).subscribe(data => {
      if (data.success) {
        this.flashMessage.show('You are registered and can login.');
        this.router.navigate(['/signin']);
      } else {
        this.router.navigate(['/signup']);
      }
    });
  }
}

2 个答案:

答案 0 :(得分:2)

好!所以我尝试了已发布的解决方案,但没有一个完全有效。我查看了文档,并意识到我忘记在模板文件(app.component.html)中添加<flash-messages></flash-messages>。这就是为什么它无法呈现并给出错误,它不是函数,因为该函数无法返回任何值。

答案 1 :(得分:1)

使用此

进行检查
  this._flashMessagesService.show('We are in about component!', { cssClass: 'alert-success', timeout: 1000 });

More on usage

Stackblitz working example