在Angular 6中,通过数组抛出错误提供路由路径

在Angular 6中,通过数组抛出错误提供路由路径,angular,angular-routing,Angular,Angular Routing,我是Angular 6应用程序,我正在从服务类传递Angular routes,格式为 { text: string, path: string } 所以我的想法是动态添加路由,如果我静态提供数据,这很好,但是如果我替换通过数组提供数据的相同结构,它会抛出错误 我坚信这与阵列结构有关,需要将其传递到router.config.unshift的方式。unshift方法接受路由器的数组,其中我将传递任意……的数组 错误 TypeError:无法读取未定义的属性“split” 在defaultUrl

我是Angular 6应用程序,我正在从服务类传递Angular routes,格式为

{ text: string, path: string }
所以我的想法是动态添加路由,如果我静态提供数据,这很好,但是如果我替换通过数组提供数据的相同结构,它会抛出错误

我坚信这与阵列结构有关,需要将其传递到router.config.unshift的方式。unshift方法接受路由器的数组,其中我将传递任意……的数组

错误 TypeError:无法读取未定义的属性“split” 在defaultUrlMatcher router.js:530

路线服务 应用程序组件 尝试:


this.router.config.unshift…dynamicRoutes

这确实有效,但并没有真正了解发生了什么以及三个点是如何起作用的。在本例中,解构赋值从数组中解压值。只需点击答案上的链接即可阅读更多内容。
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'split' of undefined
 @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}
);*/