这个问题与基于错误输出的其他问题类似,尽管我已经尝试了我能找到的所有其他答案和格式。
我有以下结构(相关文件)
- src
-- modules
--- authentication
---- login
------ login.component.ts
------ login.component.html
---- authentication.module.ts
-- app.module.ts
app.module.ts
import { HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
authentication.module.ts
import { NgModule } from '@angular/core';
import { LoginComponent } from './login/login.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
@NgModule({
declarations: [
LoginComponent
],
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule
]
})
export class AuthenticationModule { }
login.component.html
...
<form [formGroup]="form" (ngSubmit)="onSubmit()" class="form-validate is-alter" autocomplete="off">
...
然而,错误仍然存在。我也尝试过在app.module.ts上导入它,但仍然不起作用
-编辑
按照以下答案中的要求添加login.component.ts
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { AuthService } from 'src/app/services/auth.service';
import { Router, ActivatedRoute } from '@angular/router';
import { first } from 'rxjs/operators';
@Component({ selector: 'app-login', templateUrl: './login.component.html' })
export class LoginComponent implements OnInit {
form!:FormGroup;
loading = false;
submitted = false;
constructor(
private formBuilder: FormBuilder,
private authService: AuthService,
private router: Router,
private route: ActivatedRoute
) { }
ngOnInit(): void {
this.form = this.formBuilder.group({
username: [ '', [ Validators.required ] ],
password: [ '', [ Validators.required ] ]
})
}
get f() { return this.form.controls }
showingPassword = false;
loginInformation = {
username: '',
password: ''
};
onSubmit() {
this.submitted = true;
console.log( this.form.invalid );
console.log( this.f.username.errors );
console.log( this.f.password.errors );
console.log( this.form.errors );
if ( this.form.invalid ) return;
this.loading = true;
console.log( this.f.username.value );
console.log( this.f.password.value );
return;
this.authService.login( this.f.username.value, this.f.password.value )
.pipe(first())
.subscribe({
next: () => {
const returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
this.router.navigateByUrl(returnUrl);
}, error: ( error: any ) => {
console.log( error );
this.loading = false;
}
})
}
}
发布于 2021-08-27 15:42:56
为了给您一个更好的答案,我需要查看login.component.ts文件。
我猜你错过了FormBuilder的导入:
import { FormBuilder, Validators } from '@angular/forms';
你还需要在此基础上制作一个“表单”:
editForm = this.fb.group({
id: [''],
accountName: [''],
password: ['']
});
constructor(private fb: FormBuilder) {}
https://stackoverflow.com/questions/68955887
复制相似问题