我正在关注本教程:https://javebratt.com/angularfire2-email-auth/,我得到了一个奇怪的错误。
我正在登录页面实现,我已经实现了这两个文件
login.ts
import {
NavController,
LoadingController,
AlertController
} from 'ionic-angular';
import { Component } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { AuthData } from '../../providers/auth-data';
import { Page1 } from '../../pages/page1/page1';
import { SignupPage } from '../../pages/signup/signup';
import { ResetPasswordPage } from '../../pages/reset-password/reset-password';
import { EmailValidator } from '../../validators/email';
/*
Generated class for the Login page.
See http://ionicframework.com/docs/v2/components/#navigation for more info on
Ionic pages and navigation.
*/
@Component({
selector: 'page-login',
templateUrl: 'login.html'
})
export class LoginPage {
public loginForm: any;
emailChanged: boolean = false;
passwordChanged: boolean = false;
submitAttempt: boolean = false;
public loading: any;
constructor(public nav: NavController, public authData: AuthData,
public formBuilder: FormBuilder, public alertCtrl: AlertController,
public loadingCtrl: LoadingController) {
this.loginForm = formBuilder.group({
email: ['', Validators.compose([Validators.required,
EmailValidator.isValid])],
password: ['', Validators.compose([Validators.minLength(6),
Validators.required])]
});
}
goToResetPassword(){
this.nav.push(ResetPasswordPage);
}
createAccount(){
this.nav.push(SignupPage);
}
ionViewDidLoad() {
console.log('Hello LoginPage Page');
}
elementChanged(input){
let field = input.inputControl.name;
this[field + "Changed"] = true;
}
loginUser(){
this.submitAttempt = true;
if (!this.loginForm.valid){
console.log(this.loginForm.value);
} else {
this.authData.loginUser(this.loginForm.value.email,
this.loginForm.value.password).then( authData => {
this.nav.setRoot(Page1);
}, error => {
this.loading.dismiss().then( () => {
let alert = this.alertCtrl.create({
message: error.message,
buttons: [
{
text: "Ok",
role: 'cancel'
}
]
});
alert.present();
});
});
this.loading = this.loadingCtrl.create({
dismissOnPageChange: true,
});
this.loading.present();
}
}
}
和auth-data.ts
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import { AngularFire } from 'angularfire2';
@Injectable()
export class AuthData {
fireAuth: any;
constructor(public af:AngularFire) {
console.log('Hello AuthData Provider');
af.auth.subscribe(user => {
if(user){
this.fireAuth = user.auth;
console.log(user);
}
})
}
loginUser(newEmail: string, newPassword: string): any {
return this.af.auth.login({ email: newEmail, password: newPassword });
}
resetPassword(email: string): any {
return firebase.auth().sendPasswordResetEmail(email);
}
logoutUser(): any {
return this.af.auth.logout();
}
signupUser(newEmail: string, newPassword: string): any {
return this.af.auth.createUser({ email: newEmail, password: newPassword });
}
}
但是当我做'离子服务'时,我会通过login.ts
得到以下奇怪的错误属性'loginUser'在'AuthData'
类型上不存在
这个loginUser函数在我的auth-data.ts中,所以我不明白这意味着什么。
我的猜测是'this.authData'永远不会启动,但即使我在构造函数中添加this.authData = authData
,它也不会改变任何东西。 (这也说我不明白它是如何发起的)
有谁知道我为什么会收到此loginUser错误?
非常感谢
编辑:
感谢您下面的一条建议,似乎错误已经消失,而且新的一个就在这里(即使说实话,即使我还原我的更改,我仍然会收到新的错误,我很困惑)< / p>
现在我在Safari中遇到的错误是TypeError:undefined不是一个对象(评估'type.parameters'),在Firefox中:TypeError:type is undefined [En savoir plus]
Firefox控制台给了我
ReflectionCapabilitieshttp://本地主机:8100 /构建/ main.js:45824:1 Reflectorhttp://本地主机:8100 /编译/ main.js:45964:16 CompileMetadataResolverhttp://本地主机:8100 /编译/ main.js:25506:38 CompileMetadataResolverhttp://本地主机:8100 /编译/ main.js:25471:21 CompileMetadataResolverhttp:// localhost:8100 / build / main.js:25357:51地图自托管 CompileMetadataResolverhttp://本地主机:8100 /编译/ main.js:25356:65 RuntimeCompilerhttp://本地主机:8100 /编译/ main.js:40363:24 RuntimeCompilercompileModuleAndComponents http://localhost:8100/build/main.js:40301:32 RuntimeCompilerhttp://本地主机:8100 /编译/ main.js:40292:16 PlatformRefbootstrapModuleWithZone http://localhost:8100/build/main.js:27765:16 PlatformRefhttp://本地主机:8100 /编译/ main.js:27747:16 http://localhost:8100/build/main.js:96568:1 webpack_require http://localhost:8100/build/main.js:20:12 http://localhost:8100/build/main.js:66:18
我很困惑,必须错误启动某些内容,但错误堆栈并没有真正帮助...
答案 0 :(得分:0)
this.authData.loginUser(this.loginForm.value.email,
this.loginForm.value.password)
您在这里错误地访问表单参数。 loginUser函数需要2个字符串参数。
尝试:
this.loginForm.controls["email"].value
this.loginForm.controls["password"].value
编辑: 对于第二个错误,我猜这是问题所在:
resetPassword(email: string): any {
return firebase.auth().sendPasswordResetEmail(email);
}
我没有在文件中的任何位置看到firebase对象。
答案 1 :(得分:0)
我的猜测就是&#39; this.authData&#39;永远不会启动,但即使我在构造函数中添加this.authData = authData,它也不会改变任何东西。 (这也说我不明白它是如何发起的)
您不需要这样做,因为角度注入器负责提供程序的初始化。您可以阅读有关here
的更多信息关于您的问题,您是否在应用程序根模块中导入并注册了提供程序,如教程中所述?