Angular 2,如何使用 queryParams 将数组传递给路由器 [英] Angular 2, How to pass an array to the Router using queryParams

查看:26
本文介绍了Angular 2,如何使用 queryParams 将数组传递给路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 Angular 2 中获得这种 URL?

http://example.com/the-route?param[]=value1&param[]=value2&param[]=value3

我正在尝试使用 queryParamsRouter 来做它应该做的事情,但是由于 queryParams 接受一个对象,我不能这样做:

this.router.navigate(['/the-route'], queryParams: { 'param[]': 'value1', 'param[]': 'value2', 'param[]':'value3' });

因为,当然,我不能在 Object 中多次使用相同的名称(param[])

我正在为如何做到这一点而苦苦挣扎,但找不到方法

我看过这个帖子:Angular 2 将数组传递给路由器查询字符串.但是没有正确答案

解决方案

你可以这样做:

let object1 = {'name':'查尔斯','年龄':21,}让参数 = {‘学生’:JSON.stringify(object1),'职业':'法律',}this.router.navigate(['/the-route'], {queryParams: params});

当您收到学生"参数时,您只需像这样解析它:

let student = JSON.parse(params['student']);

就是这样!

Is there some way to get this kind of URL in Angular 2?

http://example.com/the-route?param[]=value1&param[]=value2&param[]=value3

I'm trying to do it like it should be, using queryParams with the Router, but as queryParams accepts an Object I can't do this:

this.router.navigate(['/the-route'], queryParams: { 'param[]': 'value1', 'param[]': 'value2', 'param[]': 'value3' });

Because, of course, I can't use the same name (param[]) several times in the Object

I'm struggling with how to do this, but can't find a way

I've seen this post: Angular 2 pass array to router queryString. But there are no correct answers

解决方案

Yo can do like this:

let object1 = {
  'name':'Charles',
  'age':21,
}

let params = {
  'student':JSON.stringify(object1),
  'carreer':'law',
}

this.router.navigate(['/the-route'], {queryParams: params});

and when you receive the 'student' param you just PARSE IT like this:

let student = JSON.parse (params['student']);

and that's it!

这篇关于Angular 2,如何使用 queryParams 将数组传递给路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆