【问题标题】:Routing on event [duplicate]事件路由[重复]
【发布时间】:2019-08-17 16:19:22
【问题描述】:

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

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 新手,所以还不能完全理解它。

【问题讨论】:

    标签: angular typescript


    【解决方案1】:
    this.router.navigate(['/first-view']);
    

    【讨论】:

      【解决方案2】:

      将路由注入组件

       import { Router } from '@angular/router';
       //...
       constructor(private router: Router){...}
      

      那你就可以了

       this.router.navigate(['a', 'b', 'c']);
      

      【讨论】:

        【解决方案3】:

        试试这个:

        import {Router} from '@angular/router';
        
        constructor(private router: Router) { }
        
        menuEvent(event: any): any {
            if (event.item.label == 'First Page') {
                this.router.navigate(['./first-view']);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-11-15
          • 2016-06-12
          • 1970-01-01
          • 2019-10-13
          • 2017-12-06
          • 2017-09-16
          • 1970-01-01
          相关资源
          最近更新 更多