我如何将所有本地存储值传递给响应 [英] how can i pass all localstorage values to response

查看:88
本文介绍了我如何将所有本地存储值传递给响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望传递fullName,lastLogon和RolePermissions,因为目前我仅获得UserId

I wish to pass fullName, lastLogon, and RolePermissions as at the moment I'm only getting UserId

抱歉,我无法直接找到答案.

Sorry I was unable to find an answer to this directly.

dashboard.service.ts

  getUsersById(id): Observable<any> {
    return this.http.get<any>(`https://www.mywebsite.net/umbraco/api/UsersApi/GetUsersById/1${id}`, {
      headers: new HttpHeaders()
        .set('Authorization', this.bearer_token)
    });
  }

  getTheUsersId(){
  this.getUsersId(localStorage.UserId).subscribe((res) => {
    console.log(res);
    this.responseId = res;
    this.StoreOnLocalStorage(res);
  })
}
  
  StoreOnLocalStorage( data: any ) {
   localStorage.setItem("fullName", data.Fullname);
   localStorage.setItem("LastLogon", data.LastLogon);
   localStorage.setItem("UserId", data.Id);
   localStorage.setItem("RolePermissions", JSON.stringify(data.UserRoles[0].RolePermissions))
}

}

dashboard.component.ts

dashboard.component.ts

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { DashboardService } from '../dashboard.service';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent implements OnInit {

  constructor( private activatedRoute: ActivatedRoute,private dashboardService:DashboardService,  ) { }
  accessToken = null;
  UserId = null;
 responseId = null;
 UserRoles = null;
 Fullname= null;

 
  ngOnInit() {
    this.dashboardService.getTheUsersId();
    this.accessToken = localStorage.getItem('accessToken');
    this.UserId = localStorage.getItem('UserId');
    this.Fullname = localStorage.getItem('FullName');
    this.UserRoles = localStorage.getItem('RolePermissions');
  } 
}

目前,我只能显示UserID,因为这只是响应中设置的内容,我希望其他值从localstorage传递

At the moment I'm only able to display the UserID as that is only what is being set in the response and I wish to have the other values passed from localstorage

我的HTML

<button  class="btn btn-primary mr-3" (click) = "getById()">Get By Id</button>
  <div class="row">
        <div class="col-6">
            <h1>Get By Id</h1>
            <p> UserID {{UserId}} </p>
            <ul *ngIf = "responseId != null">
                <li><span>Id: </span>{{responseId.Id}}</li>
                 <li><span>Fullname </span>{{responseId.Fullname}}</li>
            <li><span>Last Logon: </span>{{responseId.LastLogon}}</li>


            
  <span *ngFor="let role of responseId.UserRoles">
     Id: {{ role.Id }} - {{ role.Permission }}
     UserRole: {{role.Roles}} - {{role.UserRoles}}
 
  </span>

到目前为止,仅显示用户ID.

So far only UserID is displayed.

推荐答案

您可以从localstorage检索值并将其存储到对象中,以便以后使用.请参考以下答案,以获取方法?

You can retrieve and store values from localstorage to an object and use it later on. Please refer to below answer for How To?

获取HTML5 localStorage密钥

这篇关于我如何将所有本地存储值传递给响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆