102

Is it possible to pass multiple route params e.g. like below need to pass id1 and id2 to the component B

@RouteConfig([
    {path: '/component/:id :id2',name: 'MyCompB', component:MyCompB }
])
export class MyCompA {
  onClick(){
    this._router.navigate( ['MyCompB', {id: "someId", id2: "another ID"}]);
     }
}

5 Answers 5

102

OK realized a mistake .. it has to be /:id/:id2

Anyway didn't find this in any tutorial or other StackOverflow question.

@RouteConfig([{path: '/component/:id/:id2',name: 'MyCompB', component:MyCompB}])
export class MyCompA {
    onClick(){
        this._router.navigate( ['MyCompB', {id: "someId", id2: "another ID"}]);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

here is tutorial for same see In the routing section angular.io/docs/ts/latest/cheatsheet.html
Lot has changed since answer was published.. Which version of angular2 you are working with ? in the ng2.0.0 release build: ` const myRoutes: Routes = [ { path: 'compBPath/:id1/:id2', component: MyCompB} ]; export const myRouting: ModuleWithProviders = RouterModule.forChild(myRoutes); ` And in your NgModule ` @NgModule({ imports: [ myRouting ], declarations: [ MyCompB ], }) export class MyModule {} export class MyCompA { onClick(){ this._router.navigate( ['compBPath', "val1", "val2"]); } } `
I also agree with @user3869623, as this._router.navigate( ['MyCompB', {id: "someId", id2: "another ID"}]); denotes that 'id' and 'id2' are optional route params while in the @RouteConfig([{path: '/component/:id/:id2',name: 'MyCompB', component:MyCompB}]) :id/:id2 are required route params although they are dynamic. as, optional route params don't need to declare in the RouterConfig.
74

As detailed in this answer, mayur & user3869623's answer's are now relating to a deprecated router. You can now pass multiple parameters as follows:

To call router:

this.router.navigate(['/myUrlPath', "someId", "another ID"]);

In routes.ts:

{ path: 'myUrlpath/:id1/:id2', component: componentToGoTo},

5 Comments

For anyone who wants to know, it's also possible to add in between these ID variables some other term, which helps create a more user-friendly URL. Example: this.router.navigate(['/mypath', "firstId", "secondPath", "secondID"]) which allows for 'myPath/4/secondPath/5'
Is that also possible when using the routerlink in the template? I could not find a solution for that.
@julien-100000 - Yes it is, annotate as follows: [nsRouterLink]="['/myUrlPath', 'someId', 'another ID']"
@EamonBohan You saved my full day of investigation, you da real MVP, Thanks man!!!
I have this as my route path in app-routing.module.ts { path: :${id1}/test/:${id2}, component: DummyComponentComponent } it does not load the component for me , also additionally i have a relative path in my index.html <base href="./"> , if i just leave the path with just one param then it works but if I extend it to having /export/:${id2} it stops working , any ideas ?
20

Two Methods for Passing Multiple route params in Angular

Method-1

In app.module.ts

Set path as component2.

imports: [
 RouterModule.forRoot(
 [ {path: 'component2/:id1/:id2', component: MyComp2}])
]

Call router to naviagte to MyComp2 with multiple params id1 and id2.

export class MyComp1 {
onClick(){
    this._router.navigate( ['component2', "id1","id2"]);
 }
}

Method-2

In app.module.ts

Set path as component2.

imports: [
 RouterModule.forRoot(
 [ {path: 'component2', component: MyComp2}])
]

Call router to naviagte to MyComp2 with multiple params id1 and id2.

export class MyComp1 {
onClick(){
    this._router.navigate( ['component2', {id1: "id1 Value", id2: 
    "id2  Value"}]);
 }
}

Comments

3
      new AsyncRoute({path: '/demo/:demoKey1/:demoKey2', loader: () => {
      return System.import('app/modules/demo/demo').then(m =>m.demoComponent);
       }, name: 'demoPage'}),
       export class demoComponent {
       onClick(){
            this._router.navigate( ['/demoPage', {demoKey1: "123", demoKey2: "234"}]);
          }
        }

Comments

0

For getting the nested ids, for example this link:

this.router.navigate(['main', 'child', 1, 'grandchild', 2]);

'/main/child/1/grandchild/2'

you can get the parent's ids with the route.parent, like this:

constructor(private route: ActivatedRoute) {}

ngOnInit() {
let grandchild = this.route.snapshopt.paramMap.get('grandchild'); // 2
let child = this.route.snapshopt.parent.paramMap.get('child'); // 1
}

Comments

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.