Tengo un botón que debería llamar a una función que cierra la sesión del usuario. Por alguna razón, el evento no funciona en absoluto. Aquí está el código: Archivo TS:
import { Component, OnInit } from '@angular/core'; import { FormBuilder, FormGroup } from '@angular/forms'; import { AuthService } from 'src/app/services/auth.service'; @Component({ selector: 'app-login', templateUrl: './login.component.html', styleUrls: ['./login.component.sass'] }) export class LoginComponent implements OnInit { form: FormGroup; constructor(private formBuilder: FormBuilder, private auth: AuthService) { } ngOnInit(): void { this.form = this.formBuilder.group({ email: '', password: '' }) } submit(){ this.auth.tryLogin(this.form.getRawValue()) .subscribe(res => { console.log(res); }) } public logout(){ console.log("logout") this.auth.tryLogout() .subscribe(res => { console.log(res); }) } // public testJWT(){ // alert("asd") // this.auth.testAuth() // .subscribe(res => { // console.log(res) // }) // } }Y el HTML:
<form [formGroup]="form" (submit)="submit()"> <h1>Anmelden</h1> <input formControlName="email" type="email" placeholder="Email Addresse" required> <br> <input formControlName="password" type="password" placeholder="Passwort" required> <br> <button type="submit">Anmelden</button> </form> <button (onClick)='logout()'>log out</button>No tengo idea de lo que estoy haciendo mal, intenté usar this.logout(), intenté type="button" en el html, pero nada funcionó para mí.
Debe ser click y no onClick .
<button (click)='logout()'>log out</button>Referencia de sintaxis:
Puede consultar la wiki oficial https://angular.io/guide/event-binding para obtener más información
El evento es "clic". está utilizando onClick , que es incorrecto. Cambie onclick para click y verifique.
<button (click)='logout()'>log out </button>
Referencia: https://angular.io/guide/user-input#binding-to-user-input-events