类型为AbstractControl的参数不能分配给字符串类型的参数

时间:2018-11-15 16:05:02

标签: angular ionic3

我建立了一个表单,其中实施了表单验证,当用户将电子邮件或密码字段留空时,该表单仅显示错误消息。它工作正常,但是我的问题是我将电子邮件和密码设置为“ 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)
  }

}

1 个答案:

答案 0 :(得分:2)

好吧,错误消息说明了一切。该错误是因为signInWithEmailAndPassword需要string s,但是您用AbstractControl s来调用它。

您可以使用其上的AbstractControl属性来获取value的值。

将您的login方法更改为此:

login() {
  this.aAuth.auth.signInWithEmailAndPassword(this.email.value, this.password.value)
    .then(e => console.log(e))
}

快速提示:

不知道为什么要在组件上创建emailpassword属性时,使用this.formgroup.value可以很容易地获得表单的值,然后还可以使用解构从表单值中提取emailpassword,然后将其传递给signInWithEmailAndPassword方法:

login() {
  const { email, passowrd } = this.formgroup.value;
  this.aAuth.auth.signInWithEmailAndPassword(email, password)
    .then(e => console.log(e))
}