我被投入到一个对javascript/typescript/angular经验很少的项目中,所以请原谅我的无知.当用户登录时,我有一个post方法api调用,该调用添加了用户会话,并在成功发布后返回sessionId,它是一个字符串.我需要知道如何检索和存储该字符串以供以后在我的应用程序中使用,该字符串将用作几个不同的api调用的参数.这是我当前的服务设置:
I was put into a project where I have little experience with javascript/typescript/angular, so please excuse my ignorance. When a user logs in I have a post method api call that adds the user session and on a successful post it returns the sessionId which is a string. I need to know how to retrieve and store that string for later use in my application which will be used as a parameter for a couple different api calls. Here is my current service setup:
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { catchError, retry } from 'rxjs/operators'; import { ConfigService } from './../config/config.service'; import { UserSession } from './user-session.interface'; import { ApiServiceBase } from '../shared/api-service-base' @Injectable({ providedIn: 'root' }) export class UserSessionService extends ApiServiceBase { public userSession: UserSession; private apiRoute: string = 'user-session'; constructor(http: HttpClient, configService: ConfigService) { super(http, configService); } public async AddUserSession(userName: string): Promise<string> { this.appSettings = (await this.configService.loadConfig().toPromise()); this.userSession = { userName: userName, application: this.application}; return this.http.post<string>(this.appSettings.apiSetting.apiAddress + this.apiRoute, this.userSession) .pipe( retry(1), catchError(this.handleError) ) .toPromise() } }如何检索和存储返回的字符串?
How can I retrieve and store the returned string?
推荐答案如果即使在重新加载浏览器时也需要保留sessionId,请尝试将ID存储在浏览器的localStorage中.
If the sessionId needs to be preserved even on browser reload, try storing the id in localStorage of the browser.
localStorage.setItem("sessionId", "123");您只需使用
localStorage.getItem("sessionId");但是,如果不是这样,您还可以提供一项服务,在该服务中,您可以使用您的getter和setter来设置和获取应用程序配置数据
However if thats not the case you can also have a service where u can have your getters and setters for setting and getting app config data
app-data.service.ts
app-data.service.ts
@Injectable({ providedIn: 'root' }) export class AppDataStore { private _sessionId: string; get sessionId() { return this._sessionId; } set sessionId(sId){ this._sessionId = sId } }您可以将AppDataStore注入任何组件/服务中,并使用setter设置sessionId,也可以使用getter方法获取sessionId.
You can inject AppDataStore in any component/service and set the sessionId using the setter and also you will be able to get the sessionId using the getter method.
例如,
any-component.ts/any-services.ts
any-component.ts/any-services.ts
constructor(private userSessionService : UserSessionService, private appDataService: AppDataStore){} doSomething(){ const sessionId = await this.userSessionService.addUserSession(); this.appDataService.sessionId = sessionId }Similary为了使sessionId返回任何其他文件,您只需将AppDataService注入类的构造函数中,然后使用
Similary in order to get the sessionId back in any other file , you can just inject AppDataService in the constructor of the class and read the sessionId back using
this.appDataStore.sessionId;更多推荐
如何存储字符串以供以后通过POST方法api调用在Angular中使用?
发布评论