我如何将所有本地存储值传递给响应 [英] how can i pass all localstorage values to response
问题描述
我希望传递 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
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>
目前只显示 UserID.
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?
这篇关于我如何将所有本地存储值传递给响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!