带有矩阵网址符号的Angular2 [英] Angular2 with matrix url notation

查看:76
本文介绍了带有矩阵网址符号的Angular2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

矩阵URL表示法是使用参数创建url的默认"还是更好地将"old"表示法与参数一起使用?和&.我在angular.io文档中不了解它

Is the matrix url notation the "default" to creating urls with parameters or is better to use the "old" notation with ? and &. I didn't understand it on the angular.io documentation

localhost:3000/heroes;id=15;foo=foo

localhost:3000/heroes?id=15&foo=foo

推荐答案

矩阵参数绑定到路径段,而查询参数绑定到URL.它们具有不同的语义.使用更合适的一种. (请参阅下面另请参见"中的链接").

Matrix parameters are tied to a path segment, while query parameters are tied to the URL. They have different semantics. Use whichever one is more appropriate. (see links in "see also" below").

也许很难说,因为您总是在URL的末尾看到它,但这也是矩阵参数

Maybe it's hard to tell because you always see it at the end of the URL, but this is also matrix paramters

localhost:3000/heroes;id=15;foo=foo/bar/baz

参数绑定到heroes.当您访问route.url时,将会看到此内容

The parameters are tied to heroes. When you access the route.url, you will see this

this.route.url.subscribe((url: UrlSegment[]) => {
  let heroes = url[0];
  let heroesMatrix = heroes.parameters();
  // heroes should contain id=5, foo=foo
  let bar = url[1].path;
  let baz = url[2].path;
})

矩阵url表示法是使用参数创建url的默认",还是更好地与?一起使用旧"表示法?

Is the matrix url notation the "default" to creating urls with parameters or is better to use the "old" notation with ?

否,两者都可以使用,并且如何使用(创建)它们完全不同

No, both could be used, and how to use (create) them is completely different

    通过在数组中的路径元素之后传递一个对象,将
  • 矩阵参数绑定到每个路径段

  • matrix parameters are tied to each path segment, by passing an object after the path element in the array

router.navigate(['/foo', { id:1 }, 'bar', {baz: 2 } ])

在这里您会得到/foo;id=1/bar;baz=2

查询参数

router.navigate(['/foo'], { queryParams: { bar: 1, baz: 2 }});

在这里您会得到/foo?bar=1&baz=2

另请参见:

  • When to use query parameters versus matrix parameters?
  • URL matrix parameters vs. request parameters

如果您阅读了有关可选参数的Angular文档,他们会认为上述语义对于使用Angular并不是很重要.我可能不得不同意. REST API的语义更为重要.

If you read the Angular documentation on optional parameters, they feel that the above semantics are not really important in regards to working with Angular. I'd probably have to agree. The semantics are more important with REST APIs.

有了Angular应用程序,我们真正关心这些参数的唯一人就是我们开发人员.用户不在乎.它不是REST API,我们应该坚持众所周知的语义.对于Angular应用程序,只要我们开发人员知道如何使用参数(无论是矩阵还是查询),我们使用哪个参数都没有关系.

With an Angular app, the only people who really care about these parameters are us the developer. The user doesn't care. It is not a REST API where we should stick to well known semantics. For out Angular app, as long as we the developer know how to use params (whether matrix or query), it shouldn't matter which one we use.

话虽这么说,矩阵参数对代码的冗长程度较低,所以就我个人而言,我认为使用矩阵参数可能是最好的选择.

That being said, matrix params are less verbose to code, so personally, I'd say that using the matrix params might be the best way to go.

这篇关于带有矩阵网址符号的Angular2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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