Olá, Gabriel bom dia.
/*
Primeiramente precisamos injetar uma variável de template referente ao campo desejado.
Esta variável ao ser injetada será do tipo ElementRef. Após isso teremos acesso ao método focus()
*/
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { FormGroup,FormBuilder, Validators } from '@angular/forms';
//import { AuthService } from 'src/app/core/auth/auth.service';
import { AuthService } from '../../core/auth/auth.service';
import { Router } from '@angular/router';import { PlatformDetectorService } from 'src/app/core/platform-detector/platform-detector';
@Component({
templateUrl:'./signin.component.html'
})
export class SigninComponet implements OnInit{
loginForm:FormGroup;
/*No Angular 8, o ViewChild usa 2 parâmetros
https://angular.io/api/core/ViewChild*/
@ViewChild('userNameInput', {static: false}) userNameInput: ElementRef<HTMLInputElement>;
constructor(
private formBuilder: FormBuilder,
private authService: AuthService,
private router: Router,
private platformDetectorService: PlatformDetectorService){}
ngOnInit(): void {
this.loginForm = this.formBuilder.group({
userName: ['', Validators.required],
password:['', Validators.required]
});
this.platformDetectorService.isPlatformBrowser() &&
this.userNameInput.nativeElement.focus();
}
login(){
console.log('vai se autenticar')
const userName = this.loginForm.get('userName').value;
const password = this.loginForm.get('password').value;
this.authService
.authenticate(userName, password)
.subscribe(
() => this.router.navigate(['user', userName]),
err => {
console.log(err);
this.loginForm.reset();
this.platformDetectorService.isPlatformBrowser() &&
this.userNameInput.nativeElement.focus();
alert('Invalid user name or password');
}
);
}
}