无法获取sessionStorage值

时间:2019-07-30 05:05:56

标签: angular

am试图获取用户的sessionStorage值。但是我只能在会话存储中获取承载令牌值。

   getSessionUser() {


    let currentUser = sessionStorage.getItem('currentUser');

    const heads = new HttpHeaders({
        'Authorization': currentUser
    });

    return this.http.get<HttpResponse<Object>>(environment.apiURL + '/api/session', {headers: heads})
        .pipe(map(user => {
                sessionStorage.setItem('currentUser', JSON.stringify(user));
                return user;
            },
            error => {
                //   console.log(error);
            }));
}

1 个答案:

答案 0 :(得分:2)

您要在分配该值之前先请求currentUser

 getSessionUser() {

   const heads = new HttpHeaders({
        'Authorization': currentUser
    });

    this.http.get<HttpResponse<Object>>(environment.apiURL + '/api/session', {headers: heads})
        .pipe(map(user => {
                sessionStorage.setItem('currentUser', JSON.stringify(user));
                return user;
            },
            error => {
                //   console.log(error);
            }))
   let currentUser = sessionStorage.getItem('currentUser'); }