weixin_33699914 2016-12-01 12:46 采纳率: 0%
浏览 96

Angular2中的路径参数

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

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

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

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

  • 写回答

2条回答 默认 最新

  • weixin_33720186 2017-03-24 18:20
    关注

    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`);
    

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

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

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器