在Angular 4中使用数组参数进行路由

时间:2017-09-27 09:26:43

标签: javascript angular typescript routing routes

我目前正在尝试解决Angular 4中的路由问题。我希望URL包含参数数组,所以它看起来像这样:/#/WF001A;operationIds=146469,146470。此URL由Angular使用[routerLink]生成。需要从组件访问参数(我猜这不应该是一个问题)。

问题是 - 我不知道如何为它创建路线。我尝试了什么:

  • {path: 'WF001A/:operationIds', component: WF001AComponent}
  • {path: 'WF001A/:operationIds[]', component: WF001AComponent}
  • {path: 'WF001A:operationIds[]', component: WF001AComponent}
  • {path: 'WF001A;:operationIds[]', component: WF001AComponent}

修改 这是链接的生成方式:

<a href="#" [routerLink]="['/WF001A', {operationIds:[146469, 146470]}]">test</a>

编辑2: 组件类:

export class WF001AComponent implements OnInit {
    public title = 'WF001A';

    public constructor (private WF001AService: WF001AService, private route: ActivatedRoute) {}

    public ngOnInit(): void {
        this.route.paramMap.subscribe(params => {
            // This is never executed, for route is not recognized
            let myArray=params.getAll('operationIds');
            console.log(myArray);
        });
    }
}

1 个答案:

答案 0 :(得分:0)

我假设您的组件中有ActivatedRoute对象,例如:

constructor(
    ...,
    private route: ActivatedRoute) { }

我假设WF001A是参数名称,所以你可以这样做:

this.route.paramMap.subscribe(params => {
  let i=0;
  let myArray=params.getAll('operationIds');
}

您可以在official documentation

中找到有关可选参数的更多信息