angular - AbstractControl 类型的参数不能分配给字符串类型的参数
问题描述
我已经构建了一个表单,我在其中实现了表单验证,当用户将电子邮件或密码字段留空时,它只会显示一条错误消息。它工作得很好,但我的问题是我将电子邮件和密码设置为“AbstractControl”而不是“String”,所以当我尝试使用 Firebase 对它们进行身份验证时,我收到这条错误消息,说“AbstractControl 类型的参数不可分配给参数字符串类型”。我认为问题在于我将电子邮件和密码设置为抽象控制,而不是将其设置为字符串。我怎样才能覆盖它并允许通过验证和身份验证?这是我的代码。
HTML
<ion-content padding>
<form [formGroup]="formgroup">
<ion-list>
<ion-item>
<ion-label>Email</ion-label>
<ion-input type="text" formControlName="email" name="email"></ion-input>
</ion-item>
<ion-item *ngIf="email.hasError('required') && email.touched">
<p>*Email is required</p>
</ion-item>
<ion-item>
<ion-label>Password</ion-label>
<ion-input type="password" name="password" formControlName="password"></ion-input>
</ion-item>
<ion-item *ngIf="password.hasError('required') && password.touched">
<p>*password is required</p>
</ion-item>
</ion-list>
<button clear ion-button (click)="login()">Submit</button>
</form>
</ion-content>
TS 文件
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { AngularFireAuth } from '@angular/fire/auth';
import { SignupPage } from '../signup/signup';
import { Validators, FormBuilder, FormGroup, AbstractControl } from '@angular/forms';
/**
* Generated class for the ProfilePage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
@IonicPage()
@Component({
selector: 'page-profile',
templateUrl: 'profile.html',
})
export class ProfilePage {
email: AbstractControl;
password: AbstractControl;
formgroup: FormGroup;
constructor(public formbuilder: FormBuilder, public aAuth : AngularFireAuth, public navCtrl: NavController, public navParams: NavParams) {
this.formgroup = formbuilder.group({
email:['',Validators.required],
password:['',Validators.required]
});
this.email = this.formgroup.controls['email'];
this.password = this.formgroup.controls['password'];
}
ionViewDidLoad() {
console.log('ionViewDidLoad LoginPage');
}
login(){
this.aAuth.auth.signInWithEmailAndPassword(this.email, this.password).then(
e => {console.log(e)}
)
}
goToSignup(){
this.navCtrl.push(SignupPage)
}
}
解决方案
好吧,错误消息说明了一切。错误是因为signInWithEmailAndPassword
需要string
s 但你用AbstractControl
s 调用了它。
AbstractControl
您可以使用其上的value
属性获取 a 的值。
login
将您的方法更改为:
login() {
this.aAuth.auth.signInWithEmailAndPassword(this.email.value, this.password.value)
.then(e => console.log(e))
}
小建议:
不知道为什么要在组件上创建email
andpassword
属性,当您可以轻松获取表单的值时,this.formgroup.value
您还可以使用解构从表单值中提取email
and password
,然后将其传递给signInWithEmailAndPassword
方法:
login() {
const { email, passowrd } = this.formgroup.value;
this.aAuth.auth.signInWithEmailAndPassword(email, password)
.then(e => console.log(e))
}
推荐阅读
- r - 如何按组 ID 在连续值上创建虚拟变量?
- git - .eslintcache 文件中的合并冲突
- apache-spark - 为什么使用kafka存储cdc数据而不是直接通过spark消费?
- java - 为什么 OpenJML 不能在 for 循环中证明一个断言?
- mongodb - 是否可以复制和分片集合?
- javascript - 输入类型文件路径是假的 - JavaScript
- react-native - 在本机反应中将图像置于视图顶部,zIndex 不起作用
- angular - Angular Subject 多个不需要的订阅在组件之间进行对话
- python - 如何从python中的不同txt文件创建数据框?
- gf - GF在某些情况下无法输出介词