当用户点击“保存”按钮时,用户将被带回上一页,其中所选的列表项将被删除。那个功能就在那里。我似乎无法保存该项目。我' .get'构造函数中的项。完成后,我将数据设置为父保存功能中的保存功能,该功能在用户选择按钮时发生。感谢
storage-data.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Storage } from '@ionic/storage'
@Injectable()
export class StorageData{
constructor(public http: Http, public storage: Storage){
}
getDataFour(){
return this.storage.get('item')
}
save(data){
let newData = JSON.stringify(data);
this.storage.set('item', newData)
}
delete(data){
this.storage.remove('item');
}
}
home.ts
export class RiderPerformPage {
item: any;
saveAttempt: boolean = false;
peoples: Rider[];
constructor( public toastCtrl: ToastController, public navCtrl: NavController, private menu: MenuController, public navParams: NavParams, riders: Riders, public dataService: StorageData) {
this.rider = navParams.get('item')
this.peoples = navParams.get('items')
this.dataService.getDataFour().then((item) => {
if(item){
this.peoples = JSON.parse(item)
}
})
}
save(action){
this.action = action
this.presentToast()
this.saveAttempt = true;
this.peoples.splice(
this.peoples.indexOf(this.item), 1);
this.navCtrl.pop();
this.dataService.delete(this.peoples);
this.dataService.save(this.peoples);
}
}
答案 0 :(得分:1)
您的问题是本机存储功能删除和设置是异步的。因此,会发生以下情况:您删除该项目,但不要等到它完成并同时保存您的数据。但是因为此时删除尚未完成,所以您可以使用已删除的项目保存数据。
我认为您不必在删除项目后保存数据,因为delete命令会从商店中删除该项目。为什么你想在那之后保存?
如果您在删除数据后仍想保存,请尝试以下操作:
storage-data.ts
save(data): Promise<any> {
let newData = JSON.stringify(data);
return this.storage.set('item', newData);
}
delete(data): Promise<any> {
return this.storage.remove('item');
}
<强> home.ts 强>
save(action){
this.action = action
this.presentToast()
this.saveAttempt = true;
this.peoples.splice(
this.peoples.indexOf(this.item), 1);
this.navCtrl.pop();
this.dataService.delete(this.peoples)
.then(() => {
this.dataService.save(this.peoples);
});
}