我在我的Angular登录页面上实现了ngx-toastr。由于某些原因,成功消息起作用了,但错误没有。我尝试在成功的登录语句中使用错误消息,它起作用了,但它没有显示在失败的登录语句中。我的代码(底部的登录方法):
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { AuthService } from '../auth.service';
import { Router } from '@angular/router';
import { ViewContainerRef } from '@angular/core';
import { ToastrService } from 'ngx-toastr';
@Component({
selector: 'app-login-modal',
templateUrl: './login-modal.component.html',
styleUrls: ['./login-modal.component.css']
})
export class LoginModalComponent implements OnInit {
//Hold user data from form to send to Mongo
user = {
email: '',
password: ''
};
constructor(
private session: AuthService,
private router: Router,
public vcr: ViewContainerRef,
private toastr: ToastrService
) {
}
ngOnInit() {
}
showError() {
this.toastr.error('Username and/or password incorrect.');
}
showSuccess() {
this.toastr.success('Success!');
}
login() {
this.session.login(this.user)
.subscribe(result => {
if (result === true) {
//login successful
this.router.navigate(['/']);
this.showSuccess();
} else {
//login failed
this.showError(); <--DOESN'T WORK
console.log('result not ok', result)
}
});
}
}这似乎是一件显而易见的事情,但我也尝试过将其应用于我的服务,但它仍然不起作用。我试着改变我的else,把错误放在第一位,等等,但是什么都没有。想知道是否有人遇到过这种情况,或者是否有什么明显的东西我在这里遗漏了?
编辑:如果好奇,可以使用Angular v.5。
发布于 2018-02-11 06:37:56
因此,只要您的服务器返回401错误,您将永远不会进入result => { ... }块,因为它将被作为错误处理。按以下方式调整login()函数
login() {
this.session.login(this.user)
.subscribe(result => {
//login successful
this.router.navigate(['/']);
this.showSuccess();
}, error => {
//login failed
this.showError();
console.log('result not ok', result)
});
}它会起作用的!
https://stackoverflow.com/questions/48726259
复制相似问题