0

I Angular 6 application, I am passing angular routes from service class in format

{ text: string, path: string }

so the idea is I add routes dynamically, which works fine if I provide data statically, however if I replace same structure as providing data via array, it throw error,

I strongly believe is related to array structure, the way need to pass it to router.config.unshift. The unshift method accept array of Router where I am passing array of any.......

error

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

TypeError: Cannot read property 'split' of undefined at defaultUrlMatcher (router.js:530)

route service

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

app component

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}
);*/

1 Answer 1

1

Try destructuring assignment:

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

Sign up to request clarification or add additional context in comments.

2 Comments

this did worked but not really getting what happened and how three dots helped here
For this case, destructuring assignment unpacks values from the array. Just click the link on the answer to read more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.