所以我用Ionic和Angular做了一个简单的todo列表。您可以向列表中添加和删除内容,该列表应始终可见。到目前为止,除了在关闭应用程序时保存数据外,一切都正常。
这是我的家
export class HomePage {
name = '';
namelist = Array<string>();
constructor(public storage: Storage) {
let json = JSON.stringify(this.namelist);
localStorage.setItem('namelist', json);
var test = localStorage.getItem('namelist');
this.namelist = JSON.parse(test);
}
add() {
if (this.name.length > 0) {
let name = this.name;
this.namelist.push(name);
this.name = '';
}
}
delete(index) {
this.namelist.splice(index, 1);
}
尝试这个
export class HomePage {
name = '';
namelist = Array<string>();
constructor(public storage: Storage) {
this.getDataLocalStorage()
}
add() {
if (this.name.length > 0) {
let name = this.name;
this.namelist.push(name);
this.name = '';
}
}
delete(index) {
this.namelist.splice(index, 1);
}
updateLocalStorage(){
localStorage.setItem('namelist', JSON.stringify(this.namelist));
}
getDataLocalStorage(){
try {
const test = localStorage.getItem('namelist');
this.namelist = JSON.parse(test);
} catch (error) {
console.log(error)
}
}
}