Angular 6、通过数组提供路由路径抛出错误

In Angular 6, providing route path via array throw error

我 Angular 6 应用程序,我正在传递来自服务 class 的 angular 路由,格式为

{ text: string, path: string }

所以我的想法是我动态添加路由,如果我静态提供数据就可以正常工作,但是如果我替换与通过数组提供数据相同的结构,它会抛出错误,

我坚信与数组结构有关,需要将其传递给router.config.unshift。 unshift 方法接受路由器数组,我在其中传递任何......

错误

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'split' of undefined

TypeError: 无法读取未定义的 属性 'split' 默认 UrlMatcher (router.js:530)

航线服务

 @Injectable({
 providedIn: 'root'
 })

export class DynamicRoutingService{

messages: string[] = [];

constructor(){}

getDynamicRoutes():any{ // need fix here to pass Route[]
 return [
    { path: 'publicSurvey', component: SurveyFormComponent },
    { path: 'RedMatter', component: SurveyFormComponent },
    { path:'waterDamAndBridge', component:SurveyFormComponent}
  ];
 }
}

应用组件

export class AppComponent {

 constructor(
   private router: Router,
   private routingService:DynamicRoutingService
 ){

  let dynamicRoutes = this.routingService.getDynamicRoutes();

  this.router.config.unshift(dynamicRoutes); // this line throw error


  /*  following disable code does work
   this.router.config.unshift(
  { path: 'publicSurvey', component: SurveyFormComponent },
  { path: 'RedMatter', component: SurveyFormComponent },
  { path:'waterDamAndBridge', component:SurveyFormComponent}
);*/

尝试 destructuring assignment:

this.router.config.unshift(...dynamicRoutes);