使用AngularFire2访问firebase.storage()(Angular2 rc.5)

Vic*_*man 20 firebase firebase-storage angularfire2 angular


我试图在我的项目上访问firebase.storage(),用:

  • "@angular":"2.0.0-rc.5"
  • "angularfire2":"^ 2.0.0-beta.3-pre2"
  • "firebase":"^ 3.3.0"

我找到了这个解决方案,并创建了我的.component.ts文件:

import { Component } from '@angular/core';
declare var firebase : any;

@Component({
  template: '<img [src]="image">'
})
export class RecipesComponent {
  image: string;
  constructor() {
    const storageRef = firebase.storage().ref().child('images/image.png');
    storageRef.getDownloadURL().then(url => this.image = url);
  }
}
Run Code Online (Sandbox Code Playgroud)

我在浏览器控制台中收到以下错误:

EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in ./bstorageComponent class FBStorageComponent_Host - inline template:0:0
ORIGINAL EXCEPTION: Error: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp().
ORIGINAL STACKTRACE:
Error: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp().
Run Code Online (Sandbox Code Playgroud)

但是,我使用此导入在app.module.ts中初始化了firebase

AngularFireModule.initializeApp(firebaseConfig)
Run Code Online (Sandbox Code Playgroud)

我没有得到我所缺少的东西.如何使用firebase访问存储?使用angularfire2访问数据库没有问题.
谢谢

car*_*ant 57

在内部,AngularFire2 initializeApp在创建它时在DI工厂中调用Firebase FirebaseApp.

您正在查看错误,因为您正在访问全局firebase实例并且initializeApp尚未调用 - 因为DI基础结构不需要创建FirebaseApp或其任何依赖项.

firebase您可以注入FirebaseApp(这是Firebase initializeApp函数返回的值),而不是使用全局:

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

@Component({
  template: '<img [src]="image">'
})
export class RecipesComponent {
  image: string;
  constructor(@Inject(FirebaseApp) firebaseApp: any) {
    const storageRef = firebaseApp.storage().ref().child('images/image.png');
    storageRef.getDownloadURL().then(url => this.image = url);
  }
}
Run Code Online (Sandbox Code Playgroud)

并且,由于不再需要,您可以删除declare var firebase : any;.


早期版本的Firebase NPM模块不包含任何类型.最近的版本现在包含它们,因此firebaseApp属性可以强类型如下:

import { Component, Inject } from '@angular/core';
import { FirebaseApp } from 'angularfire2';
import * as firebase from 'firebase';

@Component({
  template: '<img [src]="image">'
})
export class RecipesComponent {
  image: string;
  constructor(@Inject(FirebaseApp) firebaseApp: firebase.app.App) {
    const storageRef = firebaseApp.storage().ref().child('images/image.png');
    storageRef.getDownloadURL().then(url => this.image = url);
  }
}
Run Code Online (Sandbox Code Playgroud)

随着AngularFire2 版本4候选版本附带的重构,FirebaseApp不再是令牌,因此可以简化注入:

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

@Component({
  template: '<img [src]="image">'
})
export class RecipesComponent {
  image: string;
  constructor(firebaseApp: FirebaseApp) {
    const storageRef = firebaseApp.storage().ref().child('images/image.png');
    storageRef.getDownloadURL().then(url => this.image = url);
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,需要显式导入,firebase/storage因为AngularFire2现在仅导入它使用的Firebase API部分.(请注意,存在支持存储的提议.)

  • @ adail.retamal如上所述,`firebaseApp`是一个构造函数参数,只能在构造函数本身中使用.如果你想在构造函数之外使用它,那么有一个简洁的替代方法:只需添加`public`或`private`,它就会被声明为成员.例如`constructor(@Inject(FirebaseApp)private firebaseApp:firebase.app.App)`,并且在构造函数之外,使用`this.firebaseApp`访问它. (2认同)

Wil*_* Wu 11

上面的代码不再有效,我一直得到'firebase.storage不是函数',尝试添加以下代码:

import * as firebase from 'firebase/app';
import 'firebase/storage';
Run Code Online (Sandbox Code Playgroud)