[英]How to properly use pouchdb in Angular2 and Ionic2?
我正在嘗試轉換為Angular 2(Ionic2),並且我的應用程序使用pouchDB。
在Angular1中,我正在使用var _usersDatabase = pouchDB(CouchConstants.COUCHDB_USERS_DB_NAME);
帶有小寫的pouchDB,沒有new
。 但是TypeScript編譯器在抱怨,所以我這樣做了:
import { Injectable } from '@angular/core';
import { LocalStorageService } from 'angular-2-local-storage';
import { UtilsService } from '../providers/utils-service';
import { MigrationService } from '../providers/migration-service';
import { CouchConstants } from '../couch-constants';
import PouchDB from 'pouchdb';
@Injectable()
export class UsersDatabase {
constructor(
private storageService: LocalStorageService
, private UtilsService: UtilsService
, private MigrationService: MigrationService
) {
'use strict';
var _usersDatabase = new PouchDB(CouchConstants.COUCHDB_USERS_DB_NAME);
//We attach the function if necessary
if (_usersDatabase.updateReplication == null) {
_usersDatabase.updateReplication = function (newDocsIds) {
console.info("Updating replications with: ");
console.info(newDocsIds);
...
但是我得到這個錯誤:
Typescript Error
Property 'updateReplication' does not exist on type 'Database<{}>'.
src/providers/users-database.ts
//We attach the function if necessary
if (_usersDatabase.updateReplication == null) {
要在TypeScript項目中導入PouchDB,您需要執行以下操作:
// Correct
import * as PouchDB from 'pouchdb';
而不是:
// Incorrect
import PouchDB from 'pouchdb';
我也不相信updateReplication
是受支持的PouchDB API,這可能就是為什么您看到TypeScript警告的原因。
我剛剛使用angular-cli安裝了PouchDB For Angular 2 + Typescript。 這是我所做的,對我有用:
ng new SOMENAME
npm install --save pouchdb
npm install --save-dev @types/pouchdb
import PouchDB from 'pouchdb';
public db: any;
並初始化this.db = new PouchDB('test'); // , {storage:'persistent'} not working in typescript without updating typings
this.db = new PouchDB('test'); // , {storage:'persistent'} not working in typescript without updating typings
參見https://github.com/nolanlawson/pouchdb-find/issues/201 。
如果在因EPERM錯誤而在Windows上安裝軟件包時遇到問題,請使用(fe) npm install --save pouchdb --no-optional
禁用警告。 安裝應該仍然可以。 有關更多信息,請參見https://github.com/npm/npm/issues/17671
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.