使用angularfire2时如何访问本机Firebase对象? [英] How to get access to native Firebase object when using angularfire2?

查看:26
本文介绍了使用angularfire2时如何访问本机Firebase对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using AngularFire2 (2.0.0-beta.2) incombination with angular2 (2.0.0-rc.4). I'd like to get access to the native firebase object (not the AngularFire root object) from Angularfire2.

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

Within my component, I want to make calls like:

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

其中 firebase 是本机 firebase 对象,就像您从下面的片段中获得的一样:

where firebase is the native firebase object, like that you get from the fragment below:

<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 来获取对象,但没有.

But I don't understand how to setup my angular2 component so that the firebase object is visible within it. Probably a very simple problem to solve, but I don't know how to solve -- I'm not an angular2 expert. I was hoping there would be and AngularFire api to get the object, but there is not.

另外,我尝试这样做的原因是我认为 angularfire2 api 还没有完成(这是可以理解的,因为它仍处于测试阶段),我正在努力解决这个问题.例如,我想更新用户的电子邮件地址或密码,或者向他们发送忘记密码的电子邮件.AngularFire2 中似乎还不存在此功能,因此我尝试使用本机 Firebase 对象来实现.

Also, the reason that I'm trying to do this is that I don't think the angularfire2 api's are complete yet (thats understandable as its still in beta) and I'm trying to work around this. For example I want to update the users email address or password, or send them the forgotten password email. None of this functionality seems to exist yet in AngularFire2, so I'm trying to implement using the native Firebase object.

推荐答案

我将尝试回答我自己的问题.首先让我说我确定我的答案不是最优雅的解决方案,我还不是 javascript/typescript/angular 专家.但我的答案确实有效——即使我不完全理解.

I'm going to try and answer my own question. Let me first say that I'm sure my answer is not the most elegant solution, I'm not yet a javascript/typescript/angular expert. But my answer does work -- even if I don't completely understand.

我使用 angular-cli 来设置我的项目,该项目基于 angular2 和最新的 firebase.显然,当您使用它来设置您的项目时,存在一个名为firebase"的全局对象.使其在 angular 2 组件中可见的一种方法是将此声明放在组件的全局级别(紧跟在 import 语句之后和类声明之前).

I used angular-cli to setup my project which is based on angular2 and the latest firebase. Apparently when you use this to setup your project there is a global object created with the name "firebase" in existence. One way to make it visible within your angular 2 component is to put this declaration at the global level in you component (right after the import statements and before your class declaration).

declare var firebase : any;

执行此操作后,全局 firebase 对象即可在您的组件中使用.

After you do this the global firebase object is available for use in your component.

这篇关于使用angularfire2时如何访问本机Firebase对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆