Ionic2 我是否需要在Ionic 2应用程序的每个页面中实例化数据库服务

Ionic2 我是否需要在Ionic 2应用程序的每个页面中实例化数据库服务,ionic2,pouchdb,Ionic2,Pouchdb,我在Typescript中将PockDB与Ionic 2一起使用 initDB(){ console.log("Initialising the DB"); this._db = new PouchDB('mydb', {adapter:'websql'}); } 每次实例化单个PageClass时,我是否必须运行这段代码?您可以为数据库创建一个全局服务。尽管没有特别使用PageDB。 您可以在提供程序构造函数中初始化db import { Injectable } fro

我在Typescript中将PockDB与Ionic 2一起使用

initDB(){
    console.log("Initialising the DB");
    this._db = new PouchDB('mydb', {adapter:'websql'});
  }

每次实例化单个PageClass时,我是否必须运行这段代码?

您可以为数据库创建一个全局服务。尽管没有特别使用PageDB。 您可以在提供程序构造函数中初始化db

import { Injectable } from '@angular/core';


@Injectable()
export class DBProvider {
 _db:any;

  constructor() {
  this._db = new PouchDB('mydb', {adapter:'websql'});
  }
...
}

在app.module.ts中将该类设置为提供程序。您可以为数据库创建全局服务。尽管尚未特别使用数据库。 您可以在提供程序构造函数中初始化db

import { Injectable } from '@angular/core';


@Injectable()
export class DBProvider {
 _db:any;

  constructor() {
  this._db = new PouchDB('mydb', {adapter:'websql'});
  }
...
}
将类设置为app.module.ts中的提供程序