2016-07-11 28 views
12

我正在使用AngularFire2(2.0.0-beta.2)与angular2(2.0.0-rc.4)相结合。我想从Angularfire2访问本机firebase对象(而不是AngularFire根对象)。如何在使用angularfire2时访问本机Firebase对象?

在我的部分,我想做出这样的呼叫:

firebase.auth().currentUser.updateEmail("[email protected]") 

其中火力是本地火力对象,就像你从下面的代码片段获取:

<script src="https://www.gstatic.com/firebasejs/3.1.0/firebase.js"></script> 
    <script> 
    // Initialize Firebase 
    // TODO: Replace with your project's customized code snippet 
    var config = { 
     apiKey: "apiKey", 
     authDomain: "projectId.firebaseapp.com", 
     databaseURL: "https://databaseName.firebaseio.com", 
     storageBucket: "bucket.appspot.com", 
    }; 
    firebase.initializeApp(config); 
    </script> 

但我不了解如何设置我的angular2组件,以使firebase对象在其中可见。可能是一个非常简单的问题来解决,但我不知道如何解决 - 我不是一个angular2专家。我希望有和AngularFire api来获取对象,但没有。

此外,我试图做到这一点的原因是,我不认为angularfire2 API是完整的(这是可以理解的,因为它仍然在测试版),我试图解决这个问题。例如,我想更新用户的电子邮件地址或密码,或向他们发送忘记的密码电子邮件。这个功能似乎还没有在AngularFire2中存在,所以我试图使用本机Firebase对象来实现。

+0

'AngularFire'用于获取3-way绑定,比如对象('$ firebaseObject')和数组('$ firebaseArray')。您可以使用常规的“Firebase”进行身份验证和其他功能 – theblindprophet

+0

如果AngularFire中尚未包含某些内容,则可以使用常规的底层Firebase JavaScript SDK。请参阅https://firebase.google.com/docs/auth/web/manage-users如果您遇到问题需要解决问题,请发布最低限度的代码,告诉我们您卡在哪里。 –

回答

6

我会尝试回答我自己的问题。首先让我说,我确定我的答案不是最优雅的解决方案,我还不是一个JavaScript /打字稿/角度专家。但我的答案确实有效 - 即使我不完全明白。

我使用angular-cli来设置我的项目,这是基于angular2和最新的firebase。显然,当你使用它来设置你的项目时,会有一个全局对象,其名称为“firebase”。在角度2组件中使其可见的一种方法是将此声明放在组件中的全局级别(紧接在导入语句之后并且在您的类声明之前)。

declare var firebase : any; 

执行此操作后,全局firebase对象可用于组件。

+2

你的解决方案工作,只有一行代码。对我来说似乎很优雅;-) –

8

如果您正在阅读这在2016年9月起,这种做法可能对你听起来不错。

见上级的理解代码:

import { Component, Inject } from '@angular/core'; 
import { AngularFire, FirebaseApp } from 'angularfire2'; 

@Component({ 
    templateUrl: 'app/auth/resetpassword.component.html' 
}) 

export class ResetpassComponent { 
    public auth: any; 
    constructor(private af: AngularFire, @Inject(FirebaseApp) firebaseApp: any) { 
    this.auth = firebaseApp.auth() 
    console.log(this.auth); 
    } 

    // formData.value.email = '[email protected]'; 
    onSubmit(formData) { 
    if(formData.valid) { 
     console.log('Sending email verification'); 
     this.auth.sendPasswordResetEmail(formData.value.email) 
     .then((response) => { 
      console.log('Sent successfully'); 
     }) 
     .catch((error) => { 
      console.log(error); 
     }) 
    } 
    } 
} 

英文:

  • 导入InjectFirebaseApp
  • 使您的组件
  • 开始全部采用原生的JavaScript SDK函数可用和方法,如sendPasswordResetEmail()

做一个auth.不与现有方法和功能自动完成,因此您可能需要接近你的文档,比如这个:https://firebase.google.com/docs/auth/web/manage-users

任何感谢?给@cartant:https://stackoverflow.com/a/39069813/1757321

相关问题