导航栏中的角度显示帐户登录

时间:2020-05-14 07:03:00

标签: angular typescript jhipster

我正在尝试获取用户的登录名,以将其放在导航栏component中。 我首先仅添加了没有eventManager的行,但是如果不刷新网页就无法看到用户名,因此我决定使用以下方法,但是我不确定如何查看广播。 因此,我决定使用jhipster构建事件管理器。我创建了broadcast和一个subscribe

有更好的方法吗?

this.eventManager.subscribe('userLoggedIn', () => {
  console.log('userLoggedIn called');
  this.accountService.identity().subscribe(account => {
     this.loggedinAccount = account.login;
  });
});

2 个答案:

答案 0 :(得分:2)

默认的JHipster模板实际上在home组件(登录页面)上显示帐户登录。我认为,如果像他们一样使用current_time - $_SESSION['clicked_time']会更容易。

getAuthenticationState()中需要进行的更改才能检索身份验证状态:

navbar.component.ts

import { Account } from 'app/core/user/account.model'; // ... export class NavbarComponent implements OnInit, OnDestroy { // ... account: Account | null = null; authSubscription?: Subscription; // constructor ngOnInit(): void { // ... this.authSubscription = this.accountService.getAuthenticationState().subscribe( account => (this.account = account) ); } ngOnDestroy(): void { if (this.authSubscription) { this.authSubscription.unsubscribe(); } } 中,您可以根据需要使用navbar.component.html,例如:

account.login

答案 1 :(得分:1)

您可以使用广播订阅,但可以使用以下内容:

loggedinAccount = 'Login';

//...
ngOnInit(): {
//...
    this.eventManager.subscribe('authenticationSuccess', () => {
        this.accountService.identity().subscribe(account => {
            this.loggedinAccount = account.login;
        });
    });
}

authenticationSuccess已经从JHipster在user-management.json中定义。 因此,只需使用它即可。

以HTML

    <span *ngIf="isAuthenticated()">
        <fa-icon icon="user"></fa-icon>
        <span>{{loggedinAccount}}</span>
    </span>