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

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

问题描述

矩阵 url 表示法是创建带参数的 url 的默认"还是更好地使用旧"表示法?和&.我在 angular.io 文档中没看懂

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

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

解决方案

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

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

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

参数绑定到heroes.当你访问route.url时,你会看到这个

this.route.url.subscribe((url: UrlSegment[]) => {让英雄 = url[0];让 heroMatrix = hero.parameters();//英雄应该包含 id=5, foo=foo让 bar = url[1].path;让 baz = url[2].path;})

<块引用>

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

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

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

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

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

  • 查询参数是通过将 NavigationExtras 作为第二个参数传递给导航来创建的

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

    这里是 /foo?bar=1&baz=2

另见:

更新(免责声明)

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

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

话虽如此,矩阵参数对代码来说不那么冗长,所以就我个人而言,我认为使用矩阵参数可能是最好的方法.

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

or

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

解决方案

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").

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

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

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 } ])
    

    Here you will get /foo;id=1/bar;baz=2

  • query parameters are created by passing the NavigationExtras as the second argument to navigate

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

    Here you get /foo?bar=1&baz=2

See also:

UPDATE (disclaimer)

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.

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.

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

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