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

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

问题描述

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



在我的组件中,我想调用如下所示的函数:

pre $ firebase.auth()。currentUser.updateEmail(user@example.com)

其中firebase是本地firebase对象,就像你从下面的片段获得的那样:

 < script src =https:// www。 gstatic.com/firebasejs/3.1.0/firebase.js\"></script> 
< script>
//初始化Firebase
// TODO:替换为您的项目的自定义代码段
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对象来实现。

解决方案

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

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

  declare var firebase:any; 

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

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")

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>

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.

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.

解决方案

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.

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;

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

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

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