我想在用户单击菜单选项时路由到特定页面。我已经创建了一个具有相应事件的方法,但我不知道如何路由到菜单上单击的特定页面。
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
},
}
这似乎很简单,但我是角度的新手,所以还没有能够理解它。
this.router.navigate(['/first-view']);
将路由器注入组件
import { Router } from '@angular/router';
//...
constructor(private router: Router){...}
然后你可以做
this.router.navigate(['a', 'b', 'c']);
试试这个:
import {Router} from '@angular/router';
constructor(private router: Router) { }
menuEvent(event: any): any {
if (event.item.label == 'First Page') {
this.router.navigate(['./first-view']);
}
}