My login function stores a token returned from api in localstorage
:
localStorage.setItem('token', res.data.token);
Soon after logging in, I need to pass this token to another function, but I can not get my variable to receive the token stored in localstorage
, I tried something like:
ngOnInit() {
let token = localStorage.getItem('token')
this.trocaToken(token);
}
However, I get:
Can not read property 'token' of undefined
If I give a console.log(localStorage.getItem('token')
, it usually prints my token, but I can not put it inside a variable.