我正在尝试解决角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);
        });
    }
}

最佳答案

我假设您的组件中有activatedroute对象,比如:

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

我假设wf001a是参数名,所以您可以执行如下操作:
this.route.paramMap.subscribe(params => {
  let i=0;
  let myArray=params.getAll('operationIds');
}

您可以在official documentation中找到有关可选参数的更多信息。

09-20 05:31