Угловой HttpErrorResponse в view.html - PullRequest
0 голосов
/ 13 мая 2018

У меня небольшая проблема, я не знаю, как я могу передать сообщение об ошибке в моем файле view.html, когда пользователь вводит неправильный пароль.

Вопрос решен

login.component.ts

export class LoginComponent implements OnInit {
   error : string;
   loginUserData = {}
   constructor(private auth: AuthService, private router: Router) { }
   ngOnInit() {
   }
   loginUser(){
      this.auth.loginUser(this.loginUserData).subscribe(
       res => {
         localStorage.setItem('token', res.token);
         this.router.navigate(['/special']);
       },
       err => {
           if( err instanceof HttpErrorResponse ) {
               if (err.status === 500) {
                   this.error = errLogin.error.message;
               }
           }
       }
       )
   }
}

login.component.html

    <div class="card-header">
      <h3 class="mb-0">Login</h3> {{ error }}
    </div>

1 Ответ

0 голосов
/ 13 мая 2018

Объявить ошибку имени переменной

error : string;

и затем присвоить значение следующим образом:

loginUser(){
      this.auth.loginUser(this.loginUserData).subscribe(
       res => {
         localStorage.setItem('token', res.token);
         this.router.navigate(['/special']);
       },
       err => {
           if( err instanceof HttpErrorResponse ) {
               if (err.status === 500) {
                   this.error = err.status
               }
           }
       }
       )
   }

и использовать в компоненте как,

<div class="card-header">
      <h3 class="mb-0">Login</h3> {{ error }}
</div>
...