How to get access to native Firebase object when using angularfire2?

放肆的年华 提交于 2019-11-28 11:01:22
Rexford

If you're reading this in September 2016 onwards, this approach might sound good to you.

See the code for superior understanding:

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);
         })
     }
  }
}

In English:

  • Import Inject and FirebaseApp
  • Make available in your component
  • Start using all the native javascript SDK functions and methods, such as sendPasswordResetEmail()

Doing a auth. doesn't autocomplete with the available methods and functions, so you might need the docs close to you, such as this: https://firebase.google.com/docs/auth/web/manage-users

Any thanks? Give to @cartant : https://stackoverflow.com/a/39069813/1757321

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.

RanchoSoftware's solution did not work for me, i used

import {AngularFireModule} from "angularfire2";
import *as firebase from 'firebase';

within the imports in the app.module.ts file

found here: https://github.com/angular/angularfire2/issues/445

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!