加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何获得当前用户ID angular 4?

发布时间:2020-12-17 17:29:21 所属栏目:安全 来源:网络整理
导读:我必须获取当前用户ID才能在我的应用程序的不同部分使用它. login.component.ts: onSignin(form: NgForm){const UserName = form.value.UserName;const Password = form.value.Password;this.authService.signinUser(UserName,Password).map(res = res.json
我必须获取当前用户ID才能在我的应用程序的不同部分使用它.

login.component.ts:

onSignin(form: NgForm){
const UserName = form.value.UserName;
const Password = form.value.Password;
this.authService.signinUser(UserName,Password).map(res => res.json())
 .subscribe(
   data => {
     if (data.Id == 0){
     this.invalid=true;
    }
    else{
      localStorage.setItem('isLoggedin','true');
      this.router.navigate(['/calendar']);
      console.log('Ok');
   }})}

auth.service.ts:

signinUser(UserName,Password): Observable<any>{
  return this.http.get('http://api.##.com/api/security/signin?username='+UserName+'&password='+Password);
}

解决方法

您可以从包含id的后端传递令牌,也可以使用redux store,它将id存储在前端供应用程序使用.以下是ng2 Redux包的角度链接.
https://www.npmjs.com/package/ng2-redux
还有一个选项可以将它保存到本地存储,但我不会喜欢它.或者
您可以使用会话存储将数据保存在前端,如下所示

在登录组件文件中保存会话使用此代码

sessionStorage.setItem('id',data.id);
retrieving from the session//
var data = sessionStorage.getItem('id');
console.log(data) to see the id in the console

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读