【发布时间】:2017-12-13 11:40:45
【问题描述】:
我是 Ionic 应用程序开发的新手,我一直在学习相同的教程。
我一直在尝试使用IonicStorageModule,但即使在 chrome 浏览器移动视图模式下运行应用程序时没有错误,也不会生成本地存储密钥。
在我的app.module.ts,我有
import { IonicStorageModule } from '@ionic/storage';
然后我在imports数组中有以下导入,
imports: [
BrowserModule,
HttpModule,
IonicModule.forRoot(MyApp),
IonicStorageModule.forRoot()
],
现在,我创建了一个服务,命名为user-settings.service.ts,
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
@Injectable()
export class UserSettings {
constructor(private storage:Storage){}
favoriteTeam(team,tournamentId, tournamentName) {
let item = { team: team,tournamentId : tournamentId, tournamentName : tournamentName};
this.storage.set(team.id , JSON.stringify(item));
}
unFavoriteTeam(team) {
this.storage.remove(team.id);
}
getAllFavorites() {
let items = [];
return this.storage.forEach((v,k,i) => {
items.push(JSON.parse(v));
}).then(() => {
return items;
});
}
}
现在在我的组件类中,我正在尝试检索所有收藏夹,但它没有按预期工作:
ionViewDidEnter() {
this.userSettings.getAllFavorites().then((val) => {
_.forEach(val,(v,k)=> {
this.favorites.push(v);
});
});
console.log(this.favorites);
}
我做错了吗?
如何从离子存储中检索所有键值?
这是正确的做法吗?对我来说似乎有很多迭代。
【问题讨论】:
标签: android angular ionic-framework ionic3