如何为生产构建angularfire2

时间:2018-06-02 02:45:57

标签: angular firebase angularfire2

在运行使用AngularFire2的Angular应用程序的生产版本后,我仍然在控制台中看到以下警告:

It looks like you're using the development build of the Firebase JS SDK.
When deploying Firebase apps to production, it is advisable to only import
the individual SDK components you intend to use.

For the module builds, these are available in the following manner
(replace <PACKAGE> with the name of a component - i.e. auth, database, etc):

CommonJS Modules:
const firebase = require('firebase/app');
require('firebase/<PACKAGE>');

ES Modules:
import firebase from 'firebase/app';
import 'firebase/<PACKAGE>';

我的应用中我唯一直接从firebase导入的位置是:

import { auth } from 'firebase';

...

constructor(private afAuth: AngularFireAuth) {}

...

login() {
 this.afAuth.auth.signInWithPopup(new auth.GithubAuthProvider())
 .then(result => {
   console.log('sign in result', result);
 })
 .catch(e => console.error('error signing in'));
}

我的项目中所有其他与firebase相关的代码都是从angularfire2导入的。我按照建议尝试导入auth

import firebase from 'firebase/app';
import 'firebase/auth';

但这并不奏效。 我做错了什么?

3 个答案:

答案 0 :(得分:3)

您必须导入firebase应用

import * as firebase from 'firebase/app';

以及您在应用程序中使用的其他任何Firebase服务

import 'firebase/auth';
import 'firebase/database';
import 'firebase/firestore';
import 'firebase/messaging';
import 'firebase/functions';

查看Firebase官方文档here

答案 1 :(得分:3)

要添加到这个问题使我整日发疯……我正在使用Angular 6和最新的AngularFire2,并根据文档进行了适当的导入。但是,我导入了Firestore来访问writeBatch类。我终于意识到我错误地使用了错误的导入,从而导致了Firebase开发SDK控制台警告。

因此要在typescript中导入诸如writeBatch之类的类,我必须进行更改:

import { firestore } from 'firebase';

收件人:

import { firestore } from 'firebase/app';

这消除了警告。我认为文档中没有提到为什么要使用“ firebase”而不是“ firebase / app”,但直到我查看js软件包文件后,对我来说,区别才是开发与生产。希望这可以帮助!

答案 2 :(得分:2)

好的,我想我明白了。这有效:

添加以下导入:

import * as firebase from 'firebase/app';
import 'firebase/auth';

并替换:

new auth.GithubAuthProvider()

使用:

new firebase.auth.GithubAuthProvider()