简体   繁体   English

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

[英]Route with array parameter in Angular 4

I'm currently trying to solve problem with routing in Angular 4. I want the URL to contain array of parameters, so it looks like this: /#/WF001A;operationIds=146469,146470 . 我目前正在尝试解决Angular 4中的路由问题。我希望URL包含参数数组,因此它看起来像这样: /#/WF001A;operationIds=146469,146470 This URL was generated by Angular using [routerLink]. 该URL是Angular使用[routerLink]生成的。 Parameters needs to be accessible from component (that shouldn't be a problem, I guess). 需要从组件访问参数(我想这应该不是问题)。

The problem is - I don't know how to create a route for it. 问题是-我不知道如何为其创建路线。 What I tried: 我试过的

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

Edit: This is how the link is generated: 编辑:这是链接的生成方式:

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

Edit 2: Component class: 编辑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);
        });
    }
}

I assume you have the ActivatedRoute object in your component, something like: 我假设您的组件中有ActivatedRoute对象,如下所示:

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

And I assume that WF001A is the parameter name, so you can do something like: 而且我假设WF001A是参数名称, 因此您可以执行以下操作:

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

You can find more information about the optional parameters in the official documentation 您可以在官方文档中找到有关可选参数的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM