SqlStorage Ionic 2 作为 service/provider
SqlStorage Ionic 2 as a service/provider
我有一个 ionic 2 的提供商,我有这个:
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()
}
我如何return一些结果才能使用?当我控制台日志 this.category
时,尝试上面的 returns 什么都没有
how to use sqlite in ionic 2 上的教程很有帮助,但无法弄清楚如何将它们转换为 services/providers。
更新(2016 年 6 月 15 日)
我在下面的原始答案中分享了一个 link。更多的讨论发生在线程上,早期的回答方法虽然有效,但可能不是最佳实践。此处更新:
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)
})
});
}
刚想到更新,可能对谁知道有用。
旧答案留下来参考
我终于定下了。在这里发帖,可能会对某人有所帮助。来自 this thread on ionic forum
的指导
在service/provider
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>
我有一个 ionic 2 的提供商,我有这个:
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()
}
我如何return一些结果才能使用?当我控制台日志 this.category
how to use sqlite in ionic 2 上的教程很有帮助,但无法弄清楚如何将它们转换为 services/providers。
更新(2016 年 6 月 15 日)
我在下面的原始答案中分享了一个 link。更多的讨论发生在线程上,早期的回答方法虽然有效,但可能不是最佳实践。此处更新:
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)
})
});
}
刚想到更新,可能对谁知道有用。
旧答案留下来参考
我终于定下了。在这里发帖,可能会对某人有所帮助。来自 this thread on ionic forum
的指导在service/provider
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>