AngularFire2 error "ReferenceError: firebase is not defined"

AngularFire2 error "ReferenceError: firebase is not defined"

我正在尝试使用 AngularFire2

创建一个 email/password 的用户

这是我在注册组件中单击提交按钮时抛出错误的代码:

firebase.auth().createUserWithEmailAndPassword(this.signupForm.value.email, this.signupForm.value.password).catch(function(error) {
  alert(error.message);
});

来自 package.json 的版本:

angular: "^2.3.1",
"angularfire2": "^2.0.0-beta.7",
"firebase": "^3.6.6"

我从一开始就在使用 angularfire2 实现 email/pass 身份验证时遇到了问题,我已经搜索了一个半多小时来寻找这个问题的答案(除其他外,没有angularfire2 似乎有很多 info/support?)

请帮忙! 我还在学习中,这真的让我难住了。

这里是控制台中的完整错误:

vendor.bundle.js:46373 ReferenceError: firebase is not defined
at SignupComponent.webpackJsonp.806.SignupComponent.signup        (http://localhost:4200/main.bundle.js:601:9)
at CompiledTemplate.proxyViewClass.View_SignupComponent0.handleEvent_0 (/AppModule/SignupComponent/component.ngfactory.js:152:34)
at CompiledTemplate.proxyViewClass.<anonymous> (http://localhost:4200/vendor.bundle.js:65473:37)
at SafeSubscriber.schedulerFn [as _next] (http://localhost:4200/vendor.bundle.js:79924:36)
at SafeSubscriber.__tryOrUnsub (http://localhost:4200/vendor.bundle.js:52413:16)
at SafeSubscriber.next  (http://localhost:4200/vendor.bundle.js:52362:22)
at Subscriber._next (http://localhost:4200/vendor.bundle.js:52315:26)
at Subscriber.next (http://localhost:4200/vendor.bundle.js:52279:18)
at EventEmitter.Subject.next (http://localhost:4200/vendor.bundle.js:52079:25)
at EventEmitter.emit (http://localhost:4200/vendor.bundle.js:79898:76)
at FormGroupDirective.onSubmit (http://localhost:4200/vendor.bundle.js:81620:23)
at Wrapper_FormGroupDirective.handleEvent (/ReactiveFormsModule/FormGroupDirective/wrapper.ngfactory.js:42:34)
at CompiledTemplate.proxyViewClass.View_SignupComponent0.handleEvent_0 (/AppModule/SignupComponent/component.ngfactory.js:150:42)
at CompiledTemplate.proxyViewClass.<anonymous> (http://localhost:4200/vendor.bundle.js:65473:37)
at HTMLFormElement.<anonymous> (http://localhost:4200/vendor.bundle.js:34579:53)

你安装了 firebase 了吗?

npm install firebase --save

我知道这是 angularfire2 包的依赖项。但是你的脚本没找到。

可能您有一个已知且已解决的问题: https://github.com/angular/angularfire2/issues/520

您可以使用 AngulareFire 通过 email/password 创建用户,例如:

import { Component } from '@angular/core';
import { AngularFire, AuthProviders, AuthMethods, AngularFireModule } from 'angularfire2';
@Component({
        ...
    })
export class MyApp {
    constructor(af: AngularFire) {
    this.af.auth.createUser({ email: email, password: password})
        .then ((user) => {
            console.log(user);
        })
        .catch((error) => {        
            console.log(error);
            });

    }
}

如果您想使用 firebase,请尝试在您的导入部分下方声明变量 firebasevar firebase = require("firebase"); // <-- Declare firebase variable here

它在我的工作组件中。让我知道结果。

所以,原来的问题如下:

我最终修改了这个代码块:

firebase.auth().createUserWithEmailAndPassword(this.signupForm.value.email, this.signupForm.value.password).catch(function(error) {                    
  alert(error.message);
});

这个:

this.af.auth.createUser(this.signupForm.value).catch(function(error) {
  alert(error.message);
});

问题在于我调用了错误的对象 firebase,而我 应该 调用了 this.af.auth在我的构造函数中定义了 public af: AngularFire

还有另一个错误,那是我在angularfire2模块上调用了错误的方法的事实:

我用过

.createUserWithEmailAndPassword(email: email, password: password)

当我应该使用

.createUser({email, password})

解决这两个问题使应用程序按预期运行,没有错误。

@sugarme 是正确的答案我刚好在离开学校之前得出同样的结论所以我没有到现在 post 这个答案的时间到了。

谢谢大家!

PS这是我的完整signup.component供参考:

import { Component, OnInit } from '@angular/core';
import { AngularFire } from 'angularfire2';
import { FormBuilder } from '@angular/forms';
import { Router } from '@angular/router';

@Component({
  selector: 'app-signup',
  templateUrl: './signup.component.html',
  styleUrls: ['./signup.component.scss']
})
export class SignupComponent {

  public signupForm = this.fb.group({
    email: [""],
    password: [""]
  });
  constructor(private router: Router, public fb: FormBuilder, public af: AngularFire) {}
  signup() {
    console.log(this.signupForm.value);
    this.af.auth.createUser(this.signupForm.value).catch(function(error) {
      alert(error.message);
    });
    this.router.navigate(['store'])
  }
}

你错过了将 firebase 导入页面

import { FirebaseProvider } from '../../providers/firebase/firebase';
import firebase from 'firebase';