首页 > 解决方案 > 事件路由

问题描述

当用户单击菜单选项时,我想路由到特定页面。我已经创建了一个带有相应事件的方法,但我不知道如何路由到在菜单上单击的特定页面。

menuEvent(event: any): any {
    if (event.item.label == 'First Page') {
         // redirect to 'first-view'
    }
  }

路由已经在app-routing模块中定义:

const routes: Routes = [
  {
    path: '',
    pathMatch: 'full',
    redirectTo: 'first-view'
  },
  {
    path: 'first-view',
    component: FirstViewComponent
  },
  {
    path: 'second-view',
    component: SecondViewComponent
  },
}

这似乎是一件非常简单的事情,但我是 Angular 的新手,所以还不能完全理解它。

标签: angulartypescript

解决方案


this.router.navigate(['/first-view']);

推荐阅读