Angular路由器:无需父URL即可调用子路由

问题描述 投票:0回答:1

我有一个这样的路由器:

const firstRoutes: Routes = [
  {
    path: '',
    children: [
      {
        path: 'parent-url',
        children: secondRoutes
      }
    ]
  }
];

和第二个这样:

export const secondRoutes: Routes = [
  {
    path: '',
    children: [
      {
        path: '',
        redirectTo: 'child-route',
        pathMatch: 'full'
      },
      {
        path: 'child-route',
        component: ChildComponent
      }
    ]
  }
];

我的ChildComponent可以通过/ child-route或/ parent-url / child-route来调用。我想禁用同时使用这两个路由调用我的函数,因此设置为只能使用/ parent-url / child-route来调用它。

我不知道如何检查是否存在/ parent-url以及是否不存在,以在没有/ parent-url的情况下禁用对ChildComponent的调用。全部网址?

angular router
1个回答
0
投票

我刚刚用angular-cli创建了一个新项目,并生成了一个childComponent。

我的路由效果很好,这是我的app-routig.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ChildComponent } from './child/child.component';

const secondRoutes: Routes = [
  {
    path: '',
    children: [
      {
        path: '',
        redirectTo: 'child-route',
        pathMatch: 'full'
      },
      {
        path: 'child-route',
        component: ChildComponent
      }
    ]
  }
];

const routes: Routes = [
  {
    path: '',
    children: [
      {
        path: 'parent-url',
        children: secondRoutes
      }
    ]
  }
];


@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule {
}
© www.soinside.com 2019 - 2024. All rights reserved.