Angular router:子路由可以不带父路由调用url

Angular router: Child route can be called without parent url

我有一台这样的路由器:

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 。整体 url?

我刚刚使用 angular-cli 创建了一个新项目并生成了一个子组件。

我的路由很好用,这是我的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 {
}