网站搜索

typescript - 无法将 PouchDB 导入 Angular 5.2.0 应用程序


我正在尝试将 PouchDB 导入到我的 Angular 应用中。我已经测试了不同的导入方法:

import PouchDB from 'pouchdb';
import * as PouchDB from 'pouchdb';

我像下面这样在一个服务中使用它:

database: PouchDB.Database = new PouchDB(DATABASE_URL);

如果我执行:import PouchDB from 'pouchdb',我会收到以下错误信息:

src/app/core/pouchdb/pouchdb.service.ts(3,8):错误 TS1192:模块 '"{PATH_OF_PROJECT}/node_modules/@types/pouchdb/index"' 没有默认导出。

如果我这样做:import * as PouchDB from 'pouchdb',我会得到以下错误消息:

错误 TypeError:PouchDB 不是构造函数,在 new PouchDBService 时

这里是不同软件包的版本:

  • PouchDB : 6.4.3
  • @types/pouchdb : 6.3.2
  • Angular 包:5.2.0
  • @angular/cli : 1.7.4

提前感谢您的回答。

答案#1

在你的 tsconfig.json 的 compilerOptions 部分添加 "allowSyntheticDefaultImports": true

并使用

import PouchDB from 'pouchdb';

答案#2

我有同样的问题。但我通过在我的 index.html 页面中添加 PouchDb CDN 解决了它

<script src="//cdn.jsdelivr.net/pouchdb/6.4.3/pouchdb.min.js"></script>

然后我就像这样在代码中使用它

declare const  PouchDB;

const _DB_ = new PouchDB('dbname');