我通过 Ionic Storage 存储一个 JSON 数组,如下所示:
this.storage.set('data', this.data).then().catch(err => {
this.data.splice(this.data.indexOf(Data), 1);
});
并在一个函数中获取它:
fetchData(){
this.storage.get('data').then(
(data: {value: string, usage: string, date: string, addedDate: string}[]) => {
this.data = data != null ? data : [];
console.log('DATA FETCHED: ' + data);
return data;
}
).catch(err => {
console.log(err);
});
}
我在数组中存储了 4 个对象,但是 console.log 只是给了我这个:
DATA FETCHED: [object Object],[object Object],[object Object],[object Object]
我应该怎么做才能正确显示数组?我已经尝试过 .toString() 和 .slice()
home.html 中的相关部分:
<ion-item *ngFor="let item of items">
<h2>{{ item.usage }}</h2>
<p>{{ item.date | date:'dd MMM yyyy' }}</p>
<h1 item-end ngClass="{{ item.value.charAt(0) === '-' ? 'expense' : 'income' }}">{{ item.value }} €</h1>
<!--<h1 item-end class="income">+ 450,00 €</h1>!-->
</ion-item>
home.ts:
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
items: any = this.moneyService.fetchData();
incomeval: any = IncomevalPage;
expenseval: any = ExpensevalPage;
constructor(public navCtrl: NavController, private moneyService: MoneyService) {}
ionViewWillEnter(){
this.items = this.moneyService.fetchData();
}
}