E
E
Earphone2018-08-19 17:04:51
Angular
Earphone, 2018-08-19 17:04:51

How to hide name in navigation menu?

How to hide the name in the navigation menu for users with a specific role?
I do it like this:

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));
  }
}

But in the console there is an error that it is impossible to read the "role" property:
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)

What did I do wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question