【发布时间】:2018-01-22 05:14:40
【问题描述】:
我现在第一次尝试在 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 = ;
*/
}
}
我如何从存储中获取值并在此处分配它们?
【问题讨论】:
-
你听说过
localStorage.setItem()和localStorage.getItem()吗?如果没有,我建议先用谷歌搜索一下 -
实例之间是否需要保存用户数据?如果没有,那么我将创建一个可注入类
标签: javascript angular typescript local-storage