问题描述
在Angular 2中是否可以通过某种方式获得这种网址?
Is there some way to get this kind of url in Angular 2?
http://www.domain.com/the-route?param[]=value1¶m[]=value2¶m[]=value3
http://www.domain.com/the-route?param[]=value1¶m[]=value2¶m[]=value3
我正在尝试将queryParams
与Router
结合使用,但应该这样做,但是当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将数组传递到路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!