Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript本地存储未清除_Javascript_Angularjs - Fatal编程技术网

Javascript本地存储未清除

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

在尝试google注销时,localStorage显示未定义的值。请帮我解决这个问题

    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