如何在Angular 6中的get url中使用localstorage值?
我想从本地存储中获取值,并在我的请求URL中使用该值来获取Angular 6中的客户详细信息 login.component.ts如何在Angular 6中的get url中使用localstorage值?,angular,Angular,我想从本地存储中获取值,并在我的请求URL中使用该值来获取Angular 6中的客户详细信息 login.component.ts OnLogin(){ this.loginService.userLogin(this.username,this.password) .subscribe( data => { localStorage.setItem('AccountNo',data.AccountNo); }
OnLogin(){
this.loginService.userLogin(this.username,this.password)
.subscribe(
data => {
localStorage.setItem('AccountNo',data.AccountNo);
}
}
login.service.ts:
getAccountNo(){
return localStorage.getItem('AccountNo');
}
checkout.service.ts:
login1 = this.loginService.getAccountNo();
async getAddress() {
const address = await this.httpClient.get<Customer[]>('http://localhost:49422/api/customer/' + 'login1' +'/profile/', { withCredentials: true })
.toPromise();
return address;
}
login1=this.loginService.getAccountNo();
异步getAddress(){
const address=wait this.httpClient.get('http://localhost:49422/api/customer/“+”login1“+”/profile/”,{withCredentials:true})
.toPromise();
回信地址;
}
客户登录后,其详细信息将保存在本地存储中。我想从本地存储中获取帐号值,并在签出服务的URL中使用。如何做到这一点?是否可以使用拦截器从请求头获取值?如果可能的话,那么应该如何实现这一点呢?可以这样使用
getAccountNo(){
return localStorage.getItem('account_no');
}
您应该使用与检索时使用的键相同的键
return localStorage.getItem('account_no');
编辑
在您的请求中,只需使用
login1
而不是'login1.AccountNo'
,我更新了我的问题。我使用了相同的键值,但没有得到结果。显示了错误,例如此url的错误请求、http错误响应。我只需要login1的值,我无法得到您。添加一个console.log(login1)并查看输出是什么“login1.AccountNo”无效您可能需要使用login1(不带引号).在进行httpRequest之前,确保login1是否有值。检查更新的问题。但是如何使用login1 as而不使用引号。这是一个url,对吗?'http://localhost:49422/api/customer/“+login1+'/profile/”,{withCredentials:true}
如果在login1
中添加引号,则没有任何意义。这是一个很小的改动。我不使用login,而是使用this.loginglad,您可以修复它。