我的身份验证服务中有这个:
constructor(private http: HttpClient, private router: Router,) {
//append headers
// set token if saved in local storage
var currentUser = JSON.parse(localStorage.getItem('user'));
this.token = currentUser && currentUser.token;
if(this.token){
this.user_id = currentUser.user.user_id;
}
}
这是下划线的:
localStorage.getItem('用户')
但得到这个错误:
错误 TS2345:类型为“string |”的参数null' 不能分配给'string' 类型的参数
我该如何解决这个问题?
Thanks
显然您需要检查 localStorage 条目中的密钥'user'
没有被设置。某事喜欢
const userJSON = localStorage.getItem('user');
if (userJSON) {
// token setting logic goes here
} else {
// plan B
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)