【发布时间】:2020-10-09 16:39:11
【问题描述】:
我有一个主页,有人在其中插入令牌,然后转到主页,然后通过按钮转到第二页。我使用一个服务在三个组件之间传递令牌,以及一个使用这个令牌调用 API 请求的服务。当我在主页或第二页并刷新页面时,令牌会丢失。我该怎么办?
首页的组件
export class HomepageComponent implements OnInit {
token:any;
a:string;
settings: any;
constructor (public parkingService: ParkingService, public root:Router, public dataService:DataService) {
}
ngOnInit(){
}
pass(passa)
{
this.a= passa.value;
this.parkingService.setUserToken(this.a);
// this.dataService.token=this.a;
var token = this.a;
localStorage.setItem("token", token);
this.dataService.setToken(localStorage.getItem("token"));
this.parkingService.getparkingDetails().subscribe(
data=>
{
// this.dataService.token=this.a;
this.root.navigate(['/pages/iot-dashboard']);
},
error => {
window.alert("Παρακαλώ δώστε έγκυρο token για το χώρο στάθμευσης");
console.error(error);
this.root.navigate(['/pages/dashboard'] );
},
);
}
}
主页面组件
export class DashboardComponent implements OnInit {
selectedMoments1 = new Date();
selectedMoments2 = new Date();
selectedMoment = new Date();
time1 = new Date();
time2 = new Date();
time3 = new Date();
getdata: any;
parkingid: string;
parkingpost: any;
allspots: any;
getdatadis: any;
allspotsdis: any;
// token:string;
snapshot:any;
selected="general";
selected2="general";
token:any;
listadis:string[];
plate:any;
listindexdisabled:number[]=[];
listindex:number[]=[];
user:any;
constructor(private parkingService: ParkingService, private route:ActivatedRoute, public root:Router , public dataservice:DataService) {
}
ngOnInit(){
this.token=this.dataservice.token;
//this.dataservice.setToken(this.token);
this.parkingService.setUserToken(this.token);
this.parkingService.getparkingDetails().subscribe(
data=>
{
this.parkingid = data._id
this.getdata = JSON.parse(data.spaces_occupied_current_slot.conventional)
this.allspots = JSON.parse(data.space_total.conventional)
this.getdatadis = JSON.parse(data.spaces_occupied_current_slot.disabled)
this.allspotsdis = JSON.parse(data.space_total.disabled)
},
error => {
window.alert("Παρακαλώ δώστε έγκυρο token για το χώρο στάθμευσης");
console.error(error);
this.root.navigate(['/pages/dashboard']);
},
);
}
........
在组件之间传递令牌的服务
import { Injectable } from '@angular/core';
import { Subject, BehaviorSubject } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
@Injectable()
export class DataService {
public token: string;
setToken(token:any){
this.token=token;
}
}
使用令牌的 API 请求服务
import {Component, OnDestroy, ErrorHandler, OnInit} from '@angular/core';
import { Injectable } from '@angular/core';
import {
BehaviorSubject,
Observable,
throwError as observableThrowError,
Subject,
} from 'rxjs';
import {
HttpClient,
HttpErrorResponse,
HttpHeaders,
} from '@angular/common/http';
import 'rxjs/add/operator/map';
import { catchError} from 'rxjs/operators';
import { NbAuthSimpleToken, NbAuthService, NbAuthSimpleInterceptor } from '@nebular/auth';
import { Router } from '@angular/router';
import {DataService} from './data.service';
@Injectable()
export class ParkingService implements OnInit{
public userToken$: Subject<string> = new BehaviorSubject<string>(null);
public token: string;
currenttoken = this.userToken$.asObservable();
constructor( private httpclient: HttpClient, private root:Router, private dataservice:DataService){
this.userToken$.subscribe( token => {
this.token = token;
console.log(this.token)
});
this.dataservice.token
}
ngOnInit(): void {
}
setUserToken(token: string) {
this.userToken$.next(token);
}
getparkingDetails(): Observable<any>{
console.log(this.token);
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Bearer: ' + this.token,
}),
};
return this.httpclient.get<any>("https://socialpark.iti.gr:8005/parking_sites/management", httpOptions).pipe(
catchError(this.errorHandler));
}
【问题讨论】:
-
使用:本地存储或会话存储
-
在此处使用 localstorage.setItem('jwt', 'your token here') 将令牌存储在服务中,然后您可以在任何地方使用 localStorage.getItem('jwt');
标签: angular service token refresh