Angular2中的路径参数 [英] Path parameters in Angular2

查看:179
本文介绍了Angular2中的路径参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对Web服务进行RESTful调用时,我们如何处理Angular 2中的路径参数?

How are we meant to handle path parameters in Angular 2 when doing RESTful calls to a Web Service?

我找到了 URLSearchParams 查询参数的对象但是从我发现的内容看来,我们必须对路径本身进行字符串连接。喜欢

I've found the URLSearchParams object for query parameters but from what I have found it seems we'll have to make do with string-concatenation for the path itself. Like

let url = 'api/v1/something/' + encodeURIComponent(someParameter) + 
          '/etc/' + encodeURIComponent(anotherParam) + '/and/so/on';

是否包含在angular2中,类似于:

Is there, included in angular2, something that does similar to:

let url = new URL('api/v1/something/{id}/etc/{name}/and/so/on', param1, param2);

我当然可以自己创建类似的东西但是如果angular2中包含某些内容则更喜欢。

I can of course create something similar myself but prefer if there is something included in angular2.

推荐答案

的确,您可以使用 id 和 name 已经设置和编码。

Indeed, you can use string interpolation and nearly exactly the same thing you suggest, assuming id and name are already set and encoded.

let url = new URL(`api/v1/something/${id}/etc/${name}/and/so/on`);

我会注意到目前 ES6 URL 类仅支持 searchParams ,但没有路径参数的概念。

I'll note that currently the ES6 URL class only supports searchParams, but has no notion of path parameters.

此外,我知道没有方法会自动编码您的参数而不实现你自己的 QueryEncoder URLSearchParams

Also, I know of no method that will automatically encode your parameters without implementing your own QueryEncoder with URLSearchParams

这篇关于Angular2中的路径参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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