如何在角度2中创建动态菜单?

时间:2016-11-15 21:50:55

标签: angular angular2-template angular2-services

我需要在用户成功登录并使用Angular 2重定向到主页后创建一个动态菜单。完全像第一个类似的问题,除了我不能硬核菜单项。

here

#app.component.html#

<navbar>
    <!--Here I want to create menu items-->
</navbar>

<div class="container">
    <div class="col-sm-12">
        <router-outlet></router-outlet>
    </div>
</div>
<div id="footer" class="inner-wrap">
    <div class="row no-padding">
        <div class="col-sm-12 no-padding">

        </div>
    </div>
</div>

**home.component.ts**

 import { Component, OnInit } from '@angular/core';

 import { User } from '../_models/index';
 import { UserService } from '../_services/index';

@Component({
//moduleId: module.id,
templateUrl: 'home.component.html'
 })

export class HomeComponent implements OnInit {
users: User[] = [];

constructor(private userService: UserService) { }

 ngOnInit() {
  <!-- how we can create it here or any better place -->
    // get users from secure api end point
    this.userService.getUsers()
        .subscribe(users => {
            this.users = users;
        });
   }

}

显然,我对这项技术不熟悉。请有人接这个吗?

块引用

3 个答案:

答案 0 :(得分:16)

我设法在用户登录后立即根据用户访问创建动态菜单。我想我无法在原始问题中正确阐述要求。昨天,在搜索“如何使两个组件相互通信”时,我在有角度的网站上发现了这一点(见下面的链接):

https://angular.io/docs/ts/latest/api/core/index/EventEmitter-class.html

我们可以通过使用Global EventEmitter来实现这一目标。以下是我在代码中实现它的方法:

GlobalEventManager:

import { Injectable, EventEmitter } from "@angular/core";

@Injectable()
export class GlobalEventsManager {
    public showNavBar: EventEmitter<any> = new EventEmitter();
    public hideNavBar: EventEmitter<any> = new EventEmitter();
}

以下链接将帮助您了解如何实施auth guard(限制用户无需登录即可进入)。

http://jasonwatmore.com/post/2016/08/16/angular-2-jwt-authentication-example-tutorial

Auth.Guard.ts:

import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { GlobalEventsManager } from "../_common/gobal-events-manager";

@Injectable()
export class AuthGuard implements CanActivate {

    constructor(private router: Router, private globalEventsManager: GlobalEventsManager) { }

    canActivate() {

        if (localStorage.getItem('currentUser')) {

            this.globalEventsManager.showNavBar.emit(true);
            return true;
        }
        else {
            // not logged in so redirect to login page
            this.router.navigate(['/login']);
            this.globalEventsManager.hideNavBar.emit(true);
            return;
        }


    }
}

menu.component.ts中使用的模型

特点:

export class Features {
    Description: string;
    RoutePath: string;
}

menu.component.ts:

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Features } from '../_models/features';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import { GlobalEventsManager } from "../_common/gobal-events-manager";

@Component({
    selector: 'nav',
    templateUrl: './menu.component.html'
})

export class MenuComponent {

    showNavBar: boolean = false;
    featureList: Features[] = [];
    private headers = new Headers({ 'Content-Type': 'application/json' });

    constructor(private http: Http, private router: Router, private globalEventsManager: GlobalEventsManager) {

        this.globalEventsManager.showNavBar.subscribe((mode: any) => {
            this.showNavBar = mode;

            if (this.showNavBar = true) {
    <!-- the below function expects user id, here I have given as 1 -->
                this.getFeatureListByLoggedInUser(1)
                    .then(list => { this.featureList = list; });
            }
        });

        this.globalEventsManager.hideNavBar.subscribe((mode: any) => {
            this.showNavBar = false;
            this.featureList = [];
        });
    }

    private getFeatureListByLoggedInUser(userID: number): Promise<Features[]> {
        return this.http.get(your api url + '/Feature/GetFeatureListByUserID?userID=' + userID)
            .toPromise()
            .then(response => response.json() as Features[])
            .catch(this.handleError);
    }

    private handleError(error: any): Promise<any> {
        console.error('An error occurred', error); // for demo purposes only
        return Promise.reject(error.message || error);
    }
}

Menu.Component.html:

<div id="navbar" *ngIf="showNavBar" class="navbar-collapse collapse navbar-collapse-custom">
    <ul class="nav navbar-nav nav_menu full-width">
        <li *ngFor="let feature of featureList" class="nav_menu" routerLinkActive="active"><a class="nav-item nav-link" [routerLink]="[feature.routepath]" routerLinkActive="active">{{feature.description}}</a></li>
    </ul>
</div>

App.Component.ts:

    <!-- menu container -->
    <nav>
    </nav>
    <!-- main app container -->
    <div class="container-fluid body-content-custom">
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 no-padding">
            <router-outlet></router-outlet>
        </div>
    </div>

    <footer class="footer">
        &nbsp;
    </footer>


In the last, we need to register the providers of menu and global event manager in app.module.ts

app.module.ts

/// <reference path="reset-password/reset-password.component.ts" />
/// <reference path="reset-password/reset-password.component.ts" />
import './rxjs-extensions';
import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpModule, XHRBackend } from '@angular/http';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import { AuthGuard } from './_guards/auth.guard';

import { ContentHeaders } from './_common/headers';
import { GlobalEventsManager } from "./_common/gobal-events-manager";
import { MenuComponent } from "./menu/menu.component";

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        HttpModule,
        AppRoutingModule,
        ReactiveFormsModule
    ],
    declarations: [
        AppComponent,
        MenuComponent
    ],
    providers: [
        AuthGuard,
        ContentHeaders,
        GlobalEventsManager
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

我希望这会有所帮助!

答案 1 :(得分:4)

这就是我的工作。基本上,流程在app组件中设置一个监听器,该组件订阅了一个可观察的getLoggedIn。当该observable发出(用户登录)时,我调用app.menu.service登录菜单。注销时,反之亦然,我得到noLoginMenu。

app.menu.service:

--force

app.component.ts

import {RouterModule, RouterLinkActive, RouterLink} from '@angular/router';
import {Injectable} from '@angular/core';
import { Observable  } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import { Subscription } from 'rxjs';


@Injectable()
export class AppMenuService {

  constructor() {

  }

  getNoLoginMenu() {
    return [
      {
        label: 'Home',
        routerLink: ['']
      },  {
        label: 'Documents',
        routerLink: ['/document']
      }, {
        label: 'Calculator',
        routerLink: ['/calculator']
      }, {
        label: 'Verify',
        routerLink: ['/verify']
      }, {
        label: 'About',
        routerLink: ['/about']
      }];
  }

  getLoggedInMenu() {
    return [
      {
        label: 'Home',
        routerLink: ['']
      }, {
        label: 'Documents',
        routerLink: ['/document']
      }, {
        label: 'Food',
        routerLink: ['/food']
      }, {
        label: 'Calculator',
        routerLink: ['/calculator']
      }, {
        label: 'Settings',
        routerLink: ['/settings']
      }, {
        label: 'Themes',
        routerLink: ['/themes']
      }, {
        label: 'About',
        routerLink: ['/about']
      }, {
        label: 'Logout',
        routerLink: ['/logout']
        //command: (event: Event) => { this.onLogout() }
      }];
  }
}

login服务:

export class AppComponent implements OnInit, OnDestroy {
    private items: MenuItem[];
    appPageHeaderDivStyle: {};
    selectedTheme: Theme;
    errorMessage: string;
    loggedIn: LoggedIn;
    loggedInEmail: string = "";
    isLoggedIn: boolean;
    themeSub: Subscription;
    loggedInSub: Subscription;
    profileSub: Subscription;

    constructor(
        private as: AppMenuService,
        private ts: ThemeService,
        private ss: SettingsService,
        private fs: FoodService,
        private ls: LoginService) {
    }

    @HostListener('window:beforeunload', ['$event'])
    beforeUnloadHander(event) {
        var shutdown = this.onShutdown();
        //event.preventDefault();

    }

    ngOnInit() {

        this.themeSub = this.ts.getNewTheme()
            .subscribe(
            theme => this.selectedTheme = theme,
            error => {
                this.errorMessage = error
            },
            () => this.completeGetNewTheme()
            );

        this.ts.setTheme("Pepper-Grinder");
        this.items = this.as.getNoLoginMenu();

        this.ls.getLoggedIn()
            .subscribe(
            loggedIn => {
                if (loggedIn.error != undefined && loggedIn.error === "" && loggedIn.email != "") {
                    this.items = this.as.getLoggedInMenu();

                    var us = this.ss.getUserSettings();
                    if (us != undefined && us.theme != null && us.theme != "") {
                        this.ts.setTheme(us.theme);
                    }


                }
                else {
                    this.items = this.as.getNoLoginMenu();
                    this.ts.setTheme("Pepper-Grinder");
                }

                this.completeLoggedIn(loggedIn.email);
            });
    }


    ngOnDestroy() {
        if (this.themeSub) {
            this.themeSub.unsubscribe();
        }

        if(this.loggedInSub) {
            this.loggedInSub.unsubscribe();
        }

        if(this.profileSub) {
            this.profileSub.unsubscribe();
        }
    }


    completeLoggedIn(email: string) {
        this.loggedInEmail = email;
        this.isLoggedIn = (this.loggedInEmail.length > 0);
    }

    completeGetNewTheme() {
        this.appPageHeaderDivStyle = this.ts.getAppPageHeaderDivStyle();
    }

    onShutdown(): boolean {
        var ok = true;
        this.fs.completeUpdateDailyFood();
        this.profileSub = this.ss.updateProfileInformation(this.ss.getUserSettings())
            .subscribe(
            status => {
                console.log("logout - updated user");
            },
            error => {
                ok = false;
            },
        );
        return ok;
    }

    onLogout() {
    }
}

答案 2 :(得分:0)

我通过基于用户权限的过滤器运行菜单项数组来实现此目的。 Array.prototype.filter()