Angular this.flashMessage.show不是一个函数

Angular this.flashMessage.show不是一个函数,angular,Angular,我在应用程序中使用angular2 flash消息进行通知。成功注册后,我必须显示用户已注册。但我面临的错误是,” this.flashMessage.show不是一个函数 " signup.component.ts: import { Component, OnInit } from '@angular/core'; import { FlashMessagesService } from 'angular2-flash-messages'; import { AuthService } fr

我在应用程序中使用angular2 flash消息进行通知。成功注册后,我必须显示用户已注册。但我面临的错误是,”

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']);
      }
    });
  }
}
用这个检查一下

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

使用此选项检查

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


好的!因此,我尝试了发布的解决方案,但没有一个完全奏效。我看了一下文档,发现我忘了在模板文件(app.component.html)中添加
。这就是它无法渲染的原因,并给出错误,即它不是函数,因为该函数无法返回任何值。

好的!因此,我尝试了发布的解决方案,但没有一个完全奏效。我看了一下文档,发现我忘了在模板文件(app.component.html)中添加
。这就是它无法呈现的原因,并给出一个错误,即它不是一个函数,因为该函数无法返回任何值。

在onSubmit()或要使用它的方法中对flashMessage进行本地声明

像这样:

var flashMessage = this.flashMessage;
然后加上 模板文件(app.component.html)中的

这对我有用。
希望能有所帮助。

在onSubmit()或您希望使用此方法的方法中对flashMessage进行本地声明

像这样:

var flashMessage = this.flashMessage;
然后加上 模板文件(app.component.html)中的

这对我有用。
希望能有所帮助。

@faceturn我明白了,但我想他刚刚从git页面添加了使用数据,我正好做了一个可行的例子,更清楚地说明了我是如何得到答案的,我没有为同样的伟大发明sameA dv,你能不能透露一下是谁给的dv@faceturn我明白了,但我认为他刚刚从git页面添加了使用数据,我正好做了一个可行的例子,更清楚地说明了我是如何得到答案的我没有为同样的伟大发明sameA dv的,你能透露是谁提供了dv吗