如何在TypeScript和Node中正确导入Firestore?

5

我正在构建一个Angular Web应用程序,我是这样导入的:

import * as firebase from 'firebase';
db = firebase.firestore();

但是最近我一直遇到这个错误:

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

Typescript:
import * as firebase from 'firebase/app';
import 'firebase/<PACKAGE>';

因此,自然而然地,我将其更改为如下内容:

import * as firebase from 'firebase/app';
import 'firebase/firestore';
db = firebase.firestore();

但错误并没有消失。我不明白如何去除它?


1
将第一行修改为从“firebase/app”导入firebase; - Indragith
1
为什么不直接使用 @angular/fire 这样的工具,它可以为您处理导入和初始化以及高级订阅等问题? - Alexander Staroselsky
当我执行 import firebase from 'firebase/app'; 时,我会收到 tslint 错误 Module '"c:/myApp/node_modules/firebase/index"' has no default export - Jus10
@AlexanderStaroselsky @angular/fire 没有一些必要的实用程序,比如获取服务器时间戳。 - Patrick
我无法评论@angular/fire中的每个具体功能,但您绝对可以创建Firebase服务器时间戳。我个人在@angular/fire项目中已经这样做了。无论哪种方式,祝编码愉快。 - Alexander Staroselsky
1个回答

3

背景

只有在加载所有 Firebase 模块时,才会触发该警告消息,就像您在第一个代码片段中所做的那样。最好采用像您更改后的裸导入方式:

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

这只导入了firestore模块。

解决方法

在您更改的任何文件中搜索导入的内容,寻找from 'firebase'。即使是import {firestore} from 'firebase';这样的代码也会触发警告消息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接