return this._http.post(apiUrl, model)
.map((res: Response) => {
console.log(res.json());
let user = res.json();
//let user = Object.keys(res.json()); <-- only gives keys not values
if (user) {
localStorage.setItem('currentUser', JSON.stringify(user));
}
})
这是我的帖子方法......
这是控制台输出......
Object {Id: 101, UserName: "appatel", FirstName: "aarsh", LastName: "patel", Password: "aarsh"…}
Email: "aarshpatel@gmail.com"
FirstName: "aarsh"
Gender: "Male "
Id: 101
LastName: "patel"
Password: "aarsh"
UserName: "appatel"
我想在本地存储中设置用户名.. 我怎么能这样做?
答案 0 :(得分:1)
只需访问属性UserName,
if (user) {
localStorage.setItem('currentUser', JSON.stringify(user.UserName));
}
答案 1 :(得分:1)
设置数据:
let user = res.json();
if (user) {
localStorage.setItem('currentUser', JSON.stringify(user));
}
获取数据:
if(localStorage.getItem('currentUser')) {
let user_data = JSON.parse(localStorage.getItem('currentUser'));
console.log(user_data);
console.log(user_data.UserName);
}