const routeConfig: Routes = [ {path: '', redirectTo: '/dashboard', pathMatch: 'full'}, {path: 'dashboard', component: DashboardComponent}, {path: 'stock', component: StockManageComponent} ];
<li *ngFor="let menu of menus" [class.active]="currentMenuId == menu.id"><a href="javascript:;" (click)="nav('menu')"><i class="fa fa-link"></i> <span>{{menu.name}}</span></a></li>
import { Component, OnInit } from '@angular/core'; import {Router} from "@angular/router"; @Component({ selector: 'app-menu', templateUrl: './menu.component.html', styleUrls: ['./menu.component.css'] }) export class MenuComponent implements OnInit { menus:Array<Menu>; currentMenuId:number; constructor(public router:Router) { } ngOnInit() { this.menus = [ new Menu(1,'首页','dashboard'), new Menu(2,'信息管理','stock') ]; } nav(menu: Menu){ this.router.navigateByUrl(menu.link); this.currentMenuId = menu.id; } } export class Menu { constructor(public id: number, public name: string, public link: string){ } }
Angular新特性,教你熟练使用 Angular 和 Typescript 开发组件式单页应用
了解课程