angular - 路由器导航到子路由 - Angular 6

标签 angular angular6 angular-router angular-routerlink

我已经这样定义了我的路线:

const routes: Routes = [
    { path: '', loadChildren: './tabs/tabs.module#TabsPageModule' },
    { path: 'faq', loadChildren: './faq/faq.module#FaqPageModule' },
    { path: 'terms', loadChildren: './terms/terms.module#TermsPageModule' }
];
@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
})
export class AppRoutingModule {}

像这样:

const routes: Routes = [
  {
    path: 'tabs',
    component: TabsPage,
    children: [
      {
        path: '',
        redirectTo: '/tabs/(list:list)',
        pathMatch: 'full',
      },
      {
        path: 'list',
        outlet: 'list',
        component: ListPage
      },
      {
        path: 'profile',
        outlet: 'profile',
        component: ProfilePage,
        canActivate: [AuthGuardService]
      }
    ]
  },
  {
    path: '',
    redirectTo: '/tabs/(list:list)',
    pathMatch: 'full'
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class TabsPageRoutingModule {}

我对如何使用组件中的路由器 .navigate() 方法或某些 html 元素中的 routerLink 导航到 ListPage 或 ProfilePage 很感兴趣。

我试过这样的东西

this.router.navigate(['tabs/profile']);

this.router.navigate(['profile']);

this.router.navigate(['tabs(profile:profile)']);

得到这个错误:

Error: Cannot match any routes. URL Segment: ''

最佳答案

尝试:

this.router.navigate(['child component path'], {relativeTo: this.activatedRoute});

这解决了我的问题。 快乐编码:)

关于angular - 路由器导航到子路由 - Angular 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53208219/

相关文章:

angular - 如何在 angular 上使用 pushState?

html - 类型 'filter' 上不存在属性 'Object'。尝试过滤响应时

Angular 将父组件的多个服务实例用于子组件

angular6 - 如何在 Angular 6 的 index.html 中使用环境变量

Angular 6 angular.json 在配置中添加 ssl

Angular 路由 "opens"新页面在前一个页面之上,并在每个路由操作中保持堆叠

angular - 获取对 ng-template 中元素的引用

angular - 如何在 Angular 2/4 中创建通用控件?

javascript - ace.edit 在 mat-tab 中找不到 div #javascript-editor

javascript - 从 Angular 6 路由器获取语言前缀作为参数