SqlStorage Ionic 2作为服务/提供程序

SqlStorage Ionic 2 as a service/provider

本文关键字:程序 服务 Ionic SqlStorage      更新时间:2023-09-26

我有一个离子二的提供者,我有这个:

getCategory() {
    this.storage.query('SELECT * FROM category')
      .then((data) => {
        var category = [];
        if (data.res.rows.length > 0) {
          for (var i = 0; i < data.res.rows.length; i++) {
            category.push({
              name: data.res.rows.item(i).name,
              type: data.res.rows.item(i).type,
              note: data.res.rows.item(i).note
            });
          }
        }
        // console.log(JSON.stringify(category)); 
        return category; // is this correct?
      }, (error) => {
        console.log('Error -> ' + JSON.stringify(error.err));
      });
  }

然后,在注入服务后,我希望在我的页面上做这样的事情:

  constructor(nav, theservice) {
    this.nav = nav;
    this.service = theservice
    this.category = service.getCategory()
  }

如何返回结果以便能够使用?当我控制台日志this.category 时,尝试以上操作不会返回任何结果

关于如何在ionic2中使用sqlite的教程很有帮助,但不知道如何将它们转换为服务/提供者。

更新(2016年6月15日)

我在下面的原始答案中分享了一个链接。更多的讨论发生在线程上,早期的回答方法虽然有效,但可能不是最佳实践。此处更新:

service.js

// using beta 7 ionic 2
import {Injectable} from '@angular/core';
import { Storage, SqlStorage } from 'ionic-angular';
@Injectable()
export class CategoryService {
  static get parameters(){
    return []
  }  
  constructor() {
    this.storage = new Storage(SqlStorage);
    this.storage.query('CREATE TABLE IF NOT EXISTS category (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, type TEXT)');
  }
  getCategory() {
    return this.storage.query('SELECT id, name, type FROM category');
  }
}

然后上面的服务是这样使用的:

somewhereInCategoryPage.js文件

 loadCategory() {
    this.platform.ready().then(() => {
      this.service.getCategory()
        .then(data => {
          this.category = [];
          if (data.res.rows.length > 0) {
            for (var i = 0; i < data.res.rows.length; i++) {
              let item = data.res.rows.item(i);
              this.category.push({
                'id': item.id,
                'name': item.name,
                'type': item.type
              });
            }
          }
          console.log(this.category);
        }, error => {
          console.log('Error', error.err)
        })
    });
  }

只是想更新一下,可能对谁知道有用。


旧答案离开此处以供参考

我终于决定了什么。在这里发帖,可能会对某人有所帮助。离子论坛上这条线索的指导

在服务/提供商中

getCategory() {
   var storage = new Storage(SqlStorage);
   return new Promise(function(resolve, reject) {
       return storage.query('SELECT name, type, note FROM category')
          .then((data) => {
             // kinda lazy workaround
             resolve(data.res.rows);
           });
        });
   }

在构造函数中:

static get parameters() {
    return [ [Myservice] ];
}
constructor(myservice) {
  myservice.getCategory()
  .then((category) => {
    // recreate new array from old category array, or else:
    // EXCEPTION: Cannot find a differ supporting object
    // https://github.com/angular/angular/issues/6392#issuecomment-171428006
    this.categories = Array.from(category);
    console.log(this.categories);
  })
  .catch((error) => {
    console.log(error);
  });
}

在模板中:

<ion-list>
    <button ion-item *ngFor="#category of categories">
        {{ category.name }}
        <br>
        <ion-icon name="arrow-forward" item-right></ion-icon>
    </button>
</ion-list>