【问题标题】:How to properly import Firestore with TypeScript and Node?如何使用 TypeScript 和 Node 正确导入 Firestore?
【发布时间】:2019-09-12 06:00:06
【问题描述】:

我正在构建一个 Angular 网络应用程序,并且我正在像这样导入:

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

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

【问题讨论】:

  • 修改第一行从'firebase/app'导入firebase;
  • 为什么不直接使用 @angular/fire 之类的东西来处理导入和初始化以及高级订阅?
  • 当我执行 import firebase from 'firebase/app'; 时,我收到 tslint 错误 Module '"c:/myApp/node_modules/firebase/index"' has no default export
  • @AlexanderStaroselsky @angular/fire 没有一些必要的实用程序,例如获取服务器时间戳。
  • 我无法评论 @angular/fire 中的每个特定功能,但您肯定会创建 firebase 服务器时间戳。我个人在@angular/fire 项目中做过。无论哪种方式,编码愉快。

标签: angular typescript firebase import google-cloud-firestore


【解决方案1】:

背景

只有当您像在第一个代码 sn-p 中一样加载所有 firebase 模块时才会触发该警告消息。最佳做法是仅使用您将其更改为的裸导入:

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

这仅导入 firestore 模块。

解决方案

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 2019-03-09
    • 2023-04-03
    • 2020-03-23
    • 2019-05-30
    相关资源
    最近更新 更多