我有登录组件。在本地存储中设置登录appId后,还需要在服务AppId对象中进行更新。该服务包含标题Key“ AppId”,该AppId的值取自登录组件或本地存储。
@Injectable({
providedIn: 'root'
})
export class WebconfigService {
baseUrl = "https://localhost:44330/api/";
AppId=""
constructor(private http: HttpClient,){
this.httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
"Accept": "application/json",
'AppId' :This.AppId
})
};
}
get(path: string,): Observable<any> {
return this.http.get(this.baseUrl+path, this.httpOptions)
.pipe(catchError(this.formatErrors));
}
SetTokenFromLogin(appId){
this.AppId=appId
}
}
登录后,AppId没有传递给服务。从登录方法中,我这样传递值。
this.authService.LoginUser(LoginForm.value).subscribe((data)=>
{ this.webConfigService.SetTokenFromLogin(data["data"]["user"]["appId"])
})
答案 0 :(得分:0)
显然,token
内部的函数表明它已将组件中的AppId
传递到服务的this.authService.LoginUser(LoginForm.value).subscribe((data)=>{
this.webConfigService.SetTokenFromLogin(data["data"]["user"]["appId"]);
})
属性中。
更正方式:
HttpOptions
更新:
据我所知,您将 contructor
设置在 AppId
内,该服务仅在服务启动时评估一次。这意味着HttpOptions
中的SetTokenFromLogin()
属性始终具有空字符串的值。
收到API响应后,您的逻辑应该在 SetTokenFromLogin(appId){
this.httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
"Accept": "application/json",
'AppId' :appId
})
};
}
内设置HttpOptions。
PinInputField