login.component.ts
angular/examples/week07-08/login.component.ts
// @ts-nocheck
import { Component, inject, signal } from '@angular/core';
import { FormBuilder, Validators, ReactiveFormsModule } from '@angular/forms';
import { AuthHttpService } from './auth-http.service';
@Component({
selector: 'app-login',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="form" (ngSubmit)="submit()" class="login">
<h1>Sign in</h1>
<label>
Email
<input formControlName="email" type="email" autocomplete="email" />
</label>
<label>
Password
<input formControlName="password" type="password" autocomplete="current-password" />
</label>
<button type="submit" [disabled]="form.invalid || loading()">Log in</button>
<p *ngIf="error()" class="error">{{ error() }}</p>
</form>
`,
styles: [
`
.login {
max-width: 24rem;
margin: 0 auto;
display: grid;
gap: 1rem;
padding: 2rem;
border-radius: 1rem;
border: 1px solid #e5e7eb;
box-shadow: 0 20px 45px rgba(15, 23, 42, 0.1);
}
label {
display: grid;
gap: 0.5rem;
}
button {
padding: 0.75rem 1.5rem;
background: #2563eb;
color: white;
border: none;
border-radius: 0.75rem;
cursor: pointer;
}
.error {
color: #dc2626;
}
`,
],
})
export class LoginComponent {
private fb = inject(FormBuilder);
private auth = inject(AuthHttpService);
loading = signal(false);
error = signal<string | null>(null);
form = this.fb.nonNullable.group({
email: ['', [Validators.required, Validators.email]],
password: ['', Validators.required],
});
submit(): void {
if (this.form.invalid) {
return;
}
this.loading.set(true);
this.error.set(null);
this.auth.login(this.form.getRawValue()).subscribe({
next: () => this.loading.set(false),
error: () => {
this.loading.set(false);
this.error.set('Invalid credentials');
},
});
}
}
相关文章
server.js
server.js — javascript source code from the angular learning materials (angular/comprehensive-demo/server/server.js).
阅读文章 →app.component.ts
app.component.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/app.component.ts).
阅读文章 →app.config.ts
app.config.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/app.config.ts).
阅读文章 →app.routes.ts
app.routes.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/app.routes.ts).
阅读文章 →auth.guard.ts
auth.guard.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/core/guards/auth.guard.ts).
阅读文章 →role.guard.ts
role.guard.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/core/guards/role.guard.ts).
阅读文章 →