我现在尝试在Angular2中第一次实现本地存储,但有些困惑。
所以,我有一个First组件,用于注册用户。
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()
吗?如果没有,我建议先去谷歌一下。 - Jeremy Thille