programing

Angular2에서 다중 경로 매개변수 전달

codeshow 2023. 8. 20. 12:56
반응형

Angular2에서 다중 경로 매개변수 전달

아래와 같은 경로 매개변수를 여러 개 통과할 수 있습니까?id1그리고.id2에게component B

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

OK 실수를 깨달았어요..그래야 한다./:id/:id2

어쨌든 이것은 튜토리얼이나 다른 StackOverflow 질문에서 발견되지 않았습니다.

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

답변에서 자세히 설명했듯이 mayur & user 3869623의 답변은 이제 사용되지 않는 라우터와 관련이 있습니다.이제 다음과 같이 여러 파라미터를 전달할 수 있습니다.

라우터를 호출하는 방법:

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

routes.ts에서:

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

Angular에서 다중 경로 매개변수를 전달하는 두 가지 방법

방법-1

app.module.ts에서

경로를 구성요소2로 설정합니다.

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

라우터를 호출하여 다중 매개변수 id1 및 id2가 있는 MyComp2로 이동합니다.

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

방법-2

app.module.ts에서

경로를 구성요소2로 설정합니다.

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

라우터를 호출하여 다중 매개변수 id1 및 id2가 있는 MyComp2로 이동합니다.

export class MyComp1 {
onClick(){
    this._router.navigate( ['component2', {id1: "id1 Value", id2: 
    "id2  Value"}]);
 }
}
      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"}]);
          }
        }

언급URL : https://stackoverflow.com/questions/36320821/passing-multiple-route-params-in-angular2

반응형