首页 > 解决方案 > 在 jhipster 的导航栏中显示用户名

问题描述

在 jhispter 应用程序中,有没有办法在导航栏中添加我们实际登录的用户 l 登录?我试图在 navbar.component.html 中添加以下内容,但没有成功。

谢谢

        <div [ngSwitch]="isAuthenticated()">
        <div class="alert alert-success" *ngSwitchCase="true">
            <span *ngIf="account" jhiTranslate="home.logged.message"
                translateValues="{username: '{{account.login}}'}"> You are logged in as user "{{account.login}}". </span>
        </div>

        <div class="alert alert-warning" *ngSwitchCase="false">
            <span jhiTranslate="global.messages.info.authenticated.prefix">If you want to </span>
            <a class="alert-link" (click)="login()" jhiTranslate="global.messages.info.authenticated.link">sign in</a></span>
        </div>
    </div>

标签: springangularjhipster

解决方案


同样对于 jhipster react 项目

在 app.tsx 中

<Header
  isAuthenticated={this.props.isAuthenticated}
  login={this.props.account.login}
  isAdmin={this.props.isAdmin}
  currentLocale={this.props.currentLocale}
  onLocaleChange={this.props.setLocale}
  ribbonEnv={this.props.ribbonEnv}
  isInProduction={this.props.isInProduction}
  isSwaggerEnabled={this.props.isSwaggerEnabled}
/>
.
.
.
const mapStateToProps = ({ authentication, applicationProfile, locale }: IRootState) => ({
  currentLocale: locale.currentLocale,
  isAuthenticated: authentication.isAuthenticated,
  account: authentication.account,
  isAdmin: hasAnyAuthority(authentication.account.authorities, [AUTHORITIES.ADMIN]),
  ribbonEnv: applicationProfile.ribbonEnv,
  isInProduction: applicationProfile.inProduction,
  isSwaggerEnabled: applicationProfile.isSwaggerEnabled
});

在 header.tsx

const { currentLocale, isAuthenticated, login, isAdmin, isSwaggerEnabled, isInProduction } = this.props;
.
.
.
<AccountMenu login={login} isAuthenticated={isAuthenticated} closeNav={this.closeNav} />

在 account.tsx 中

export const AccountMenu = props => (
  <NavDropdown
    closeNav={props.closeNav}
    icon="user"
    name={props.isAuthenticated ? props.login : translate('global.menu.account.main')}
    id="account-menu"
  >
    {props.isAuthenticated ? accountMenuItemsAuthenticated : accountMenuItems}
  </NavDropdown>
);

推荐阅读