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

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

问题描述

在Angular 2中是否可以通过某种方式获得这种网址?

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

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

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

我正在尝试将queryParamsRouter结合使用,但应该这样做,但是当queryParams接受对象时,我不能这样做:

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' });

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

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

我正在努力做到这一点,但找不到方法

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

我看过这篇文章: 到路由器queryString的角度2传递数组.但是没有正确的答案

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

推荐答案

您可以这样做:

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']);

就是这样!

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

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