Earphone
@Earphone

Как скрыть имя в меню навигации?

Как скрыть имя в меню навигации для пользователей c определенной ролью?

Делаю так:

import { Component, OnInit } from '@angular/core';
import {AuthService } from "../../services/auth/auth.service";

export interface RouteInfo {
    path: string;
    title: string;
    icon: string;
    class: string;
    role: string[];
}

export const ROUTES: RouteInfo[] = [
    { path: 'notes',  title: 'Заметки',  icon: 'ti-comment', class: '', role:['Пользователь', 'Администратор'] },
    { path: 'contacts', title: 'Контакты',  icon:'ti-info', class: '', role:['Пользователь', 'Администратор'] },
    { path: 'users', title: 'Пользователи',  icon:'ti-user', class: '', role:['Пользователь', 'Администратор'] },
    { path: 'admins', title: 'Администрирование',  icon:'ti-server', class: '', role:['Администратор'] }
];


@Component({
  selector: 'app-sidebar',
  templateUrl: './sidebar.component.html',
  styleUrls: ['./sidebar.component.css']
})

export class SidebarComponent implements OnInit {
  public menuItems: any[];
  constructor(
    public authService:AuthService
) {
}
  ngOnInit() {
    this.menuItems = ROUTES.filter(menuItem => menuItem.role.includes(this.authService.currentUserData.role));
  }
}


Но в консоле возникает ошибка о том что невозможно прочитать свойство "роль":

ERROR TypeError: Cannot read property 'role' of undefined
at eval (sidebar.component.ts:39)
at Array.filter ()
at SidebarComponent.ngOnInit (sidebar.component.ts:39)
at checkAndUpdateDirectiveInline (core.es5.js:10843)
at checkAndUpdateNodeInline (core.es5.js:12341)
at checkAndUpdateNode (core.es5.js:12284)
at debugCheckAndUpdateNode (core.es5.js:13141)
at debugCheckDirectivesFn (core.es5.js:13082)
at Object.eval [as updateDirectives] (LayoutComponent.html:3)
at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:13067)


Что не так я сделал?
  • Вопрос задан
  • 41 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы