如何处理在角2查询参数 [英] How to handle query parameters in angular 2
问题描述
在我的路由组件
我
@RouteConfig {
{path: '/login', name: 'Login', component: LoginComponent}
}
但我如何获得查询参数如果我去 APP_URL /登录?标记= 1234
?
推荐答案
要补充原有的2 previous答案,Angular2支持路由中同时查询参数和路径变量。在 @RouteConfig
定义,如果路径中定义的参数,Angular2处理它们的路径变量和查询参数如果不是。
To complement the two previous answers, Angular2 supports both query parameters and path variables within routing. In @RouteConfig
definition, if you define parameters within a path, Angular2 handles them as path variables and as query parameters if not.
让我们来示例:
@RouteConfig([
{ path: '/:id', component: DetailsComponent, name: 'Details'}
])
如果您调用导航
这样的路由器的方法:
If you call the navigate
method of the router like this:
this.router.navigate( [
'Details', { id: 'companyId', param1: 'value1'
}]);
您将有以下地址:<?code> / companyId参数1 =值。获取参数的方式是两种,查询参数和路径变量一样。它们之间的区别在于,路径变量可以被看作是强制性的参数和查询参数作为任选的。
You will have the following address: /companyId?param1=value1
. The way to get parameters is the same for both, query parameters and path variables. The difference between them is that path variables can be seen as mandatory parameters and query parameters as optional ones.
希望它可以帮助你,
蒂埃里
Hope it helps you, Thierry
这篇关于如何处理在角2查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!