Passing Multiple route params in Angular2

前端 未结 4 1483
耶瑟儿~
耶瑟儿~ 2021-01-31 00:51

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

@RouteConfig([
           


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-31 01:43

    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"}]);
     }
    }
    

提交回复
热议问题