router.navigate with query params Angular 5 [英] router.navigate with query params Angular 5

查看:298
本文介绍了router.navigate with query params Angular 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了使用查询参数路由到路由的问题我有类似的功能

I'm having an issue with routing to a route with query params I have a function like so

goToLink(link) {
    this.router.navigate([`${link.split('?')[0]}`, { queryParams: this.sortParams(link)}]);
}

此函数

sortParams(link) {
    let queryParams = url.split('?')[1];
    let params = queryParams.split('&');
    let pair = null;
    let data = {};
    params.forEach((d) => {
      pair = d.split('=');
      data[`${pair[0]}`] = pair[1];
    });
    return data;
}

好吧基本上发生了什么我有一个名为 goToLink()并且它接收一个url并且传递的url是一个带有查询参数的字符串,如此...

okay so basically what Is happening I have a function called goToLink() and that takes in a url and the url that gets passed is a string with query params like so..

https://website.com/page?id=37&username=jimmy

以上只是一个例子不是它实际上是什么样的,但它现在是一个带有查询参数的链接字符串,所以会发生什么呢?我从字符串中删除params并将它们存储在 sortParams()函数所以当我传递上面的字符串时,我得到一个看起来像这样的对象

the above is just an example thats not what it actually looks like but its a link string with query parameters now so what happens is I remove the params from the string and store them in a data object in the sortParams() function so when I pass the above string in I get an object that looks like this

{id:37,用户名:'jimmy'}

现在这就是我传递到路由器中 queryParams:部分的内容.navigate,

now thats what i'm passing into the queryParams: section in the router.navigate,

返回对象时该函数应该如下所示

the function should look like this when the object is returned

this.router.navigate([`${link.split('?')[0]}`, { queryParams: {id: 37, username: 'jimmy'}}]);

所以我成功路由到所需的路线,但查询参数看起来像这样..

so i successfully route to the desired route but the query params look like this..

/ page; queryParams =%5Bobject%20Object%5D

我在这里做错了吗?

任何帮助都将不胜感激!

Any help would be appreciated!

EDIT

如果我只是将功能改为此

If I just change the function to this

 this.router.navigate([`${link.split('?')[0]}`, { queryParams: {id: 37, username: 'jimmy'}}]);

我得到相同的网址 / page; queryParams =%5Bobject%20Object% 5D

推荐答案

你可以放置一个据称是第一个参数的支架但是你把它封装在整条路线上

Can be of that you had placed the bracket which is supposedly for the 1st param but you had encapsulated it on the whole line of route

你的代码:

// This is the end of your route statement:  '}}]);' which the close bracket is included
this.router.navigate([`${link.split('?')[0]}`, { queryParams: {id: 37, username: 'jimmy'}}]);

更新路线

仅将] 关闭括号放在第一个参数中,尽量不要将其放在路线对帐的最后一部分。

place the ] close bracket within the 1st parameter only, try to not place it on the last part of the route statement.

// Update end line: '}});'
this.router.navigate([`${link.split('?')[0]}`], { queryParams: {id: 37, username: 'jimmy'}});

这篇关于router.navigate with query params Angular 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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