K.Z
K.Z

Reputation: 5075

How to pass additional route parameter in angular?

I am working on Angular 6 application and have route survey that is config on app root level and then :id to take it survey detail page, I am trying to navigate from component using

 this.router.navigate(['/survey'], this.listedSurvey.surveyIdNum);

But I believe I am missing something from router navigate as I am unable to do so.

App route

const routes: Routes = [
{ path:'welcome', component:WelcomeComponent },
{ path:'', redirectTo: 'welcome', pathMatch:'full'},
{ path:'survey', loadChildren: '../survey/survey.module#SurveyModule' },
{ path:'**', component:PageNotFoundComponent}
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [
 RouterModule
 ]
 })

 export class AppRoutingModule { }

survey module

const routes: Routes = [
{ 
    path:'', 
    component:SurveyComponent,
},
{
    path:':id',
    component: SurveyFormComponent
 }
];

@NgModule({
 imports:[
     RouterModule.forChild(routes)
 ],
 exports:[]
})
 export class SurveyRouting{
}

component

 private handleSelectedSurvey(dataItem:any){
   this.listedSurvey = dataItem;
   const a:number = 2;
   //this.router.navigate(['/survey'], this.listedSurvey.surveyIdNum);
   this.router.navigate(['/survey'],{queryParams:{id:a}}); 
 }

Upvotes: 0

Views: 34

Answers (1)

Ininiv
Ininiv

Reputation: 1325

Change the navigate() this.router.navigate(['/survey', this.listedSurvey.surveyIdNum]);

Refer: https://angular.io/api/router/Router#navigate

Upvotes: 1

Related Questions