Javascript本地存储未清除
在尝试google注销时,localStorage显示未定义的值。请帮我解决这个问题Javascript本地存储未清除,javascript,angularjs,Javascript,Angularjs,在尝试google注销时,localStorage显示未定义的值。请帮我解决这个问题 ngOnInit(): void { this.socialAuthService.authState.subscribe((user) => { this.user = user; localStorage.setItem('googleUserName', JSON.stringify(this.user?.name)); this.myStore
ngOnInit(): void {
this.socialAuthService.authState.subscribe((user) => {
this.user = user;
localStorage.setItem('googleUserName', JSON.stringify(this.user?.name));
this.myStore = localStorage.getItem('googleUserName')
})
if(localStorage.getItem('googleUserName')) {
this.myStore = localStorage.getItem('googleUserName')
this.newUser = JSON.parse(this.myStore);
this.user = {
"name": this.newUser
}
}
}
// google signout
signOutWithGoogle() {
localStorage.clear()
this.socialAuthService.signOut()
}
检查您是否在任何地方调用signOutWithGoogle函数,我没有在任何地方调用此函数。如果在signOutWithGoogle中没有行“this.socialAuthService.signOut()”,本地存储将在触发signOutWithGoogle后被清除。请确认
signOutWithGoogle()
在应该触发时被触发。可能是console.log?是的,此函数在触发时工作,但使本地存储未定义。可能您应该异步运行它:async/await
?
try to change in your code -
localStorage.removeItem('googleUserName');
your code will be updated like
// google signout`enter code here`
signOutWithGoogle() {
localStorage.removeItem('googleUserName');
this.socialAuthService.signOut()
}
https://www.w3schools.com/jsref/met_storage_removeitem.asp