Angular 5 - 添加了动态路由但未路由

Angular 5 - Dynamic Route Added but not routing

我创建了一个新项目并向路由模块添加了一些代码以进行动态路由:

路由模块代码如下:

import { NgModule } from '@angular/core';
import { Routes, RouterModule, Router} from '@angular/router';

import { OneComponent } from './one/one.component';
import { HomeComponent } from './home/home.component';

const routes: Routes = [
  { path: 'home', component: HomeComponent },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule {
  constructor(router: Router, routerModule: RouterModule) {
    console.log('Routes: ', JSON.stringify(routes, undefined, 1));
    routes.push({path: 'new/random/path', component: OneComponent});
    routes.forEach((x, i) => {
      console.log(`${i}: ${JSON.stringify(x, undefined, 1)}`);
    });
  }
}

以及 app.component.html 上的示例链接

<a routerLink="home">Home</a>

<a routerLink="new/random/path">Dynamic</a>

<router-outlet></router-outlet>

问题是,虽然新路由已成功推送到路由数组,但我收到此错误:

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'new/random/path'
Error: Cannot match any routes. URL Segment: 'new/random/path'

我该如何解决这个问题?

试试这样的东西:

  constructor(router: Router) {
    const config = router.config;
    config.push({path: 'new/random/path', component: OneComponent});
    router.resetConfig(config);
  }