Отображение имени пользователя в навигационной панели в jhipster - PullRequest
0 голосов
/ 16 мая 2018

В приложении jhispter, есть ли способ добавить в панель навигации пользователя логин мы на самом деле логин?Я попытался добавить следующий фрагмент в 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>

Ответы [ 3 ]

0 голосов
/ 03 февраля 2019

Аналогично для проекта Jhipster реагирует

В 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>
);
0 голосов
/ 04 февраля 2019

Обновите navbar.component.ts следующим ....

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';

import { VERSION } from 'app/app.constants';
import { Principal, LoginModalService, LoginService, Account } from 'app/core';
import { ProfileService } from '../profiles/profile.service';
import { JhiEventManager } from 'ng-jhipster';

@Component({
    selector: 'jhi-navbar',
    templateUrl: './navbar.component.html',
    styleUrls: ['navbar.scss']
})
export class NavbarComponent implements OnInit {
    inProduction: boolean;
    isNavbarCollapsed: boolean;
    languages: any[];
    swaggerEnabled: boolean;
    modalRef: NgbModalRef;
    version: string;
    account: Account;

    constructor(
        private loginService: LoginService,
        private principal: Principal,
        private loginModalService: LoginModalService,
        private profileService: ProfileService,
        private router: Router,
        private eventManager: JhiEventManager
    ) {
        this.version = VERSION ? 'v' + VERSION : '';
        this.isNavbarCollapsed = true;
    }

    ngOnInit() {
        this.profileService.getProfileInfo().then(profileInfo => {
            this.inProduction = profileInfo.inProduction;
            this.swaggerEnabled = profileInfo.swaggerEnabled;
        });
        this.principal.identity().then(account => {
            this.account = account;
        });
        this.registerAuthenticationSuccess();
    }

    registerAuthenticationSuccess() {
        this.eventManager.subscribe('authenticationSuccess', message => {
            this.principal.identity().then(account => {
                this.account = account;
            });
        });
    }

    collapseNavbar() {
        this.isNavbarCollapsed = true;
    }

    isAuthenticated() {
        return this.principal.isAuthenticated();
    }

    login() {
        this.modalRef = this.loginModalService.open();
    }

    logout() {
        this.collapseNavbar();
        this.loginService.logout();
        this.router.navigate(['']);
    }

    toggleNavbar() {
        this.isNavbarCollapsed = !this.isNavbarCollapsed;
    }

    getImageUrl() {
        return this.isAuthenticated() ? this.principal.getImageUrl() : null;
    }
}

Обновите home.component.html до чего-то вроде ....

<span *ngIf="!isAuthenticated() || account == null">
 <fa-icon icon="user"></fa-icon>
<span>
   Sign in
</span>
</span>
<span *ngIf="account && isAuthenticated()">
<span>
 {{ account.login }}
</span>
</span>
0 голосов
/ 16 мая 2018

Добавить объект типа Account внутри navbar.component.ts.

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';

import { ProfileService } from '../profiles/profile.service';
import {Principal, LoginModalService, LoginService, Account} from '../../shared';

import { VERSION } from '../../app.constants';

@Component({
    selector: 'jhi-navbar',
    templateUrl: './navbar.component.html',
    styleUrls: [
        'navbar.css'
    ]
})
export class NavbarComponent implements OnInit {
    inProduction: boolean;
    isNavbarCollapsed: boolean;
    languages: any[];
    swaggerEnabled: boolean;
    modalRef: NgbModalRef;
    version: string;
    account: Account;

    constructor(
        private loginService: LoginService,
        private principal: Principal,
        private loginModalService: LoginModalService,
        private profileService: ProfileService,
        private router: Router
    ) {
        this.version = VERSION ? 'v' + VERSION : '';
        this.isNavbarCollapsed = true;
    }

    ngOnInit() {
        this.profileService.getProfileInfo().then((profileInfo) => {
            this.inProduction = profileInfo.inProduction;
            this.swaggerEnabled = profileInfo.swaggerEnabled;
        });
        this.principal.identity().then((account) => {
            this.account = account;
        });
    }

    collapseNavbar() {
        this.isNavbarCollapsed = true;
    }

    isAuthenticated() {
        return this.principal.isAuthenticated();
    }

    login() {
        this.modalRef = this.loginModalService.open();
    }

    logout() {
        this.collapseNavbar();
        this.loginService.logout();
        this.router.navigate(['']);
    }

    toggleNavbar() {
        this.isNavbarCollapsed = !this.isNavbarCollapsed;
    }

    getImageUrl() {
        return this.isAuthenticated() ? this.principal.getImageUrl() : null;
    }
}

Попробуйте что-то вроде этого в html:

<span>
  Account
  <span *ngIf="account"> ({{account.login}})</span>
</span>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...