gpt4 book ai didi

angular5 - TypeError : _this. flashMessagesService.show 不是函数

转载 作者:行者123 更新时间:2023-12-04 16:07:29 25 4
gpt4 key购买 nike

尝试使用 angular2-flash-messages 并得到控制台错误 _this.flashMessagesService.show is not a function

按照 https://github.com/moff/angular2-flash-messages 执行的步骤

应用程序模块.ts

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

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

signin.component.ts 有

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

signin.component.ts 构造函数有:

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']);
});
}

得到错误_this.flashMessagesService.show is not a function

最佳答案

   // 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
});

在这里你可以查看https://github.com/moff/angular2-flash-messages/issues/35

关于angular5 - TypeError : _this. flashMessagesService.show 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48130967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com