我尝试首次在Angular2中实现一个本地存储,并且有点困惑.
所以,我有第一个组件,我注册我的用户
export class FormComponent {
modus = ['first', 'second'];
model: User = new User('', '', '');
constructor(private _cookieService: CookieService) {}
}
这是我在FormComponent中使用的类
export class User {
constructor (
public email: string,
public name: string,
public modus: string
) {}
}
我绑定它,形式上的一切都很好.
现在我想将它存储在本地存储中(User的3个参数)
但我怎么能这样做?
我离开这个页面注册用户并转到其他页面,如
export class Part1Component {
public email;
public name;
public modus;
constructor(private _location: Location) {}
myTestFunction(){
/* assign values here
this.email = ;
this.name = ;
this.modus = ;
*/
}
}
我如何从存储中获取值并在此处分配它们?
解决方法:
如果您使用Json.stringify()将用户对象存储在会话存储中,那么您可以这样做
let user = Json.parse(localStorage.getItem('user'));
this.email=user.email;
this.name=user.name;
原文地址:https://codeday.me/bug/20190724/1522599.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。