gpt4 book ai didi

firebase - 使用angularfire2时如何访问 native Firebase对象?

转载 作者:行者123 更新时间:2023-12-04 00:41:22 24 4
gpt4 key购买 nike

我将 AngularFire2 (2.0.0-beta.2) 与 angular2 (2.0.0-rc.4) 结合使用。我想从 Angularfire2 访问 native firebase 对象(不是 AngularFire 根对象)。

在我的组件中,我想进行如下调用:

firebase.auth().currentUser.updateEmail("user@example.com")

其中 firebase 是 native firebase 对象,就像您从下面的片段中获得的一样:
<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 中似乎还不存在此功能,因此我尝试使用 native Firebase 对象来实现。

最佳答案

如果您从 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 = 'your@email.com';
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

    关于firebase - 使用angularfire2时如何访问 native Firebase对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38310842/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com