【发布时间】:2019-08-25 01:30:12
【问题描述】:
我试图将数据存储在一个数组中以便稍后在其他组件中获取它,但这会引发错误:
ERROR TypeError: Cannot read property 'push' of undefined
我使用本地存储。
onSubmit() {
let email = this.AuthForm.get("email").value;
let password = this.AuthForm.get("password").value;
let nomcomplet = "";
let occupation = "";
let niveau = 0;
let newUser = new User(email, password, nomcomplet, occupation, niveau);
console.log(newUser);
let val: any = [];
val = this.storage.get(this.key);
val.push(newUser.email);
this.storage.set(this.key, JSON.stringify(val));
}
我希望在本地存储中找到所有存储的值,但实际是错误的,并且之前只存储了最后一个值
截屏
【问题讨论】:
-
console.log(newuser) 返回什么?
-
当你从 localStorage 获取数组时,使用
JSON.parse(localStorage.getItem())。 -
嗨,谢谢你,但很抱歉,它没有工作
标签: angular local-storage web-storage