如何在Angular 6中获取url中的localstorage值?

问题描述 投票:2回答:2

我想从本地存储中获取价值,并在我的请求URL中使用该值来获取Angular 6中的客户详细信息。

login.component.ts

  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;
        }

客户登录后,其详细信息将保留在本地存储中。我想从本地存储中获取帐号值,并在结帐服务​​中的URL中使用。怎么做到这一点?是否可以使用拦截器从请求标头中获取值?如果有可能那么应该如何实现呢?

angular
2个回答
3
投票

可以这样使用,

 getAccountNo(){
        return localStorage.getItem('account_no');
      }

3
投票

你应该使用你用来检索的same key

  return localStorage.getItem('account_no');

编辑

你需要在你的请求中使用login1而不是'login1.AccountNo'

© www.soinside.com 2019 - 2024. All rights reserved.