【问题标题】:Angular router: Child route can be called without parent urlAngular 路由器:可以在没有父 url 的情况下调用子路由
【发布时间】:2020-05-12 08:52:03
【问题描述】:

我有一个这样的路由器:

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
      }
    ]
  }
];

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

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

【问题讨论】:

  • 我也有同样的问题。你找到原因了吗?

标签: angular router


【解决方案1】:

我刚刚用 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 {
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    相关资源
    最近更新 更多