3

我正在处理登录页面。我想要做的是我在一个类中有两个函数validateLogin()submit(). 单击登录按钮时,我已成功提交,并且可以获取用户名和密码。

我试图在我的提交函数中调用我的 validateLogin 函数,但我无法调用它,我的 sublime 文本在 validateLogin 上显示错误,并且我的 chrome 浏览器中没有任何错误。

import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {FormBuilder,Validators} from "@angular/common";

@Component({
  templateUrl: 'build/pages/home/home.html'
})

export class HomePage {

    public loginForm: any;
  public commentOnTimeOut: any;

  constructor(private navCtrl: NavController, public form:FormBuilder) {
    this.loginForm = this.form.group({
        "email":["",Validators.required],
        "password":["",Validators.required]
    })
  }

  validateLogin(cb){
    window.setTimeout( () => {
        if (this.loginForm.value.email == 'admin' && this.loginForm.value.password == 'admin123') {
          console.log("invoking If part");
          cb('valid User');
        }
        else {
          console.log("invoking elsePart");
          cb('Invalid User');
        }
    }, 3000);
  }

  submit(){

    console.log(this.loginForm.value);
    console.log(this.loginForm.value.email);
    console.log(this.loginForm.value.password);

    validateLogin(this.loginForm.value.email, this.loginForm.value.password, (response) => {
      console.log(response);
      this.commentOnTimeOut = response;
    })

    this.commentOnTimeOut = "Validating User";
    console.log(this.commentOnTimeOut);
  }  

}

如何在我的提交函数中调用我的函数 validateLogin?

这是一个演示 plunker,它运行良好

错误:

在我的控制台中获取http://localhost:8100/build/js/app.bundle.js

在我的发球中,我得到了类似的错误TypeScript error: /home/panini/AutoSparParts/app/pages/home/home.ts(40,5): Error TS2663: Cannot find name 'validateLogin'. Did you mean the instance member 'this.validateLogin'?

上述错误我正在调用任何帮助

4

2 回答 2

3

要访问你需要this.在任何班级成员之前使用的班级成员,在你的情况下试试这个

this.validateLogin(this.loginForm.value.email, this.loginForm.value.password, (response) => {
   console.log(response);
   this.commentOnTimeOut = response;
})
于 2016-08-04T07:49:05.010 回答
3

由于您使用的是 a的实例class,因此如果要引用该实例内的属性或方法,则必须this.在属性或方法名称之前添加 。

在我的服务中,我收到类似 TypeScript 错误的错误:/home/panini/AutoSparParts/app/pages/home/home.ts(40,5): Error TS2663: Cannot find name 'validateLogin'。您的意思是实例成员“this.validateLogin”吗?

那是因为如果不添加thisvalidateLogin方法中,将查找具有该名称的全局方法,并且因为它不存在,所以会引发错误。所以你的submit方法应该是:

submit(){

    console.log(this.loginForm.value);
    console.log(this.loginForm.value.email);
    console.log(this.loginForm.value.password);

    this.validateLogin(this.loginForm.value.email, this.loginForm.value.password, (response) => {
      console.log(response);
      this.commentOnTimeOut = response;
    })

    this.commentOnTimeOut = "Validating User";
    console.log(this.commentOnTimeOut);
  } 

更新:

是的,我试试这个。我收到类似 TypeScript 错误的错误:/home/panini/AutoSparParts/app/pages/home/home.ts(40,5): Error TS2346: Supplied parameters do not match any signature of call target.

那是因为您的validateLogin方法只需要一个参数(cb参数),而您要发送三个参数(电子邮件、密码和回调)。

尝试像这样修改该行:

 // New method to handle the response
 processLoginResult(response) {
    console.log(response);
    this.commentOnTimeOut = response;
 }

 submit(){

    console.log(this.loginForm.value);
    console.log(this.loginForm.value.email);
    console.log(this.loginForm.value.password);

    // Instead of calling it with email, password and the callback
    // just send the added method as a callback 
    this.validateLogin(this.processLoginResult);

    this.commentOnTimeOut = "Validating User";
    console.log(this.commentOnTimeOut);
}
于 2016-08-04T09:40:22.173 回答