首页 文章

Angular 2 Routing中的重复查询参数

提问于
浏览
3

我正在将现有的应用程序转换为Angular 2,并且要求查询参数可以出现零次或多次(即重复)次 . 一个例子是一个url,它有多个参数用于排序(如 #/results/data;sort=column1;sort=column2 )或调用Solr实例并指定多个facet过滤(https://cwiki.apache.org/confluence/display/solr/Common+Query+Parameters#CommonQueryParameters-Thefq(FilterQuery)Parameter) . 通常,接收 endpoints 识别那些重复的参数并将它们呈现为数组 .

如何使用Angular 2路由进行此操作?我正在使用RC4和3.0-beta-2路由器 . 当我订阅 ActivatedRoute 并查看 params 时,只返回最后一个重复的值,但我知道正在传递完整的URL,因为 Location.path() 显示了键入的内容 .

例如,给出以下URL

https://localhost:44300/#/search;fq=efg:456;q=xyz;fq=abc

console.log(this.location.path(false)); 显示 /search;fq=efg:456;q=xyz;fq=abc

但只返回 Object {fq: "abc", q: "xyz"}

this.route.params.subscribe(params => {
   console.log(params);
});

3 回答

  • 2

    update

    这已经支持了一段时间了 .

    orginal

    目前不支持此功能 .
    有一个未解决的问题是最终获得对多个值的支持 .

    https://github.com/angular/angular/issues/9477

  • 1

    更新:现在应该是fixed .

    this.route.params.subscribe(params => {
       // assuming ?foo=a&foo=b&bar=c
       // should return an object {'foo': ['a', 'b'], 'bar': 'c'}
       console.log(params);
    });
    
  • 0

    为了在视图之间传递复杂数据,我认为您更适合使用注入每个组件而不是查询参数的服务 .

相关问题