简体   繁体   English

Angular2中的路由器参数

[英]Router Parameter in Angular2

The problem is that router parameters are showing in URL while I want my router parameter not to show in URL ? 问题是路由器参数在URL中显示,而我希望路由器参数不在URL中显示?

myComponent.ts myComponent.ts

@RouteConfig([      
        { path: '/routerOne/:myId', component: routerOne, name: "routerOne",useAsDefault: true},
        { path: '/routerTwo/:myId', component: routerTwo, name: "routerTwo"},

])

myComponent.html myComponent.html

  <a [routerLink]="['routerOne',{myId:this.Id}]">RouterOne</a>
  <a [routerLink]="['routerTwo',{myId:this.Id}]">routerTwo</a>

As of now sharedService can be the last option as you don't want to show data in url . 到目前为止, sharedService可能是最后一个选择,因为您不想在url显示数据。 RouteData can be an option but it is immutable . RouteData可以是一个选项,但它是immutable please read this two threads carefully. 请仔细阅读这两个主题。

https://github.com/angular/angular/issues/6672 https://github.com/angular/angular/issues/6672

https://github.com/angular/angular/issues/6569 https://github.com/angular/angular/issues/6569

If you want to send static value via routing as parameters then you can send via data property of routing in angular2 by using this parameters does't show in the URL. 如果要通过路由将静态值作为参数发送,则可以使用URL中未显示的此参数通过angular2中路由的data属性发送。

Basically There are two options (upto my knowledge) to send data via routing 基本上,有两种选择(据我所知)可以通过路由发送数据

  • RouteParams (as used in question) RouteParams (有问题)
  • data(RouteData) (property at the time of routing) data(RouteData) (路由时的属性)

RouteParams RouteParams

Now when we send data using RouteParams we have to define in the similer way like this: 现在,当我们使用RouteParams发送数据时,我们必须以更类似的方式进行定义,如下所示:

{path: '/editUser/:userId', name: 'Edit User', component: UserEditComponent}

<a href="#" [routerLink]="['Edit User',{userId: id}]"

By using this method we send data normaly but all data is visible in the URL 通过使用此方法,我们可以正常发送数据,但所有数据在URL中都是可见的

Data 数据

when we don't want to show data in the URL path than we have to send data via routing using data property of the @RouteConfig annotation provided by angualr2. 当我们不想在URL路径中显示数据时,我们必须使用@RouteConfig annotation提供的@RouteConfig annotation data属性通过路由发送数据。 by using this property we can send additional data to the components at the time of the route configuration without showing it in the URL. 通过使用此属性,我们可以在路由配置时将其他数据发送到组件,而无需在URL中显示。 here is example of this property. 这是此属性的示例。

@RouteConfig([
    {path: '/product/:id', component: ProductDetailComponentParam,
     as: 'ProductDetail', data: {isProd: true}}])

export class ProductDetailComponentParam {
    productID: string;
    constructor(params: RouteParams, data: RouteData) {
        this.productID = params.get('id');

        console.log('Is this prod environment', data.get('isProd'));
    }
}

by using this we can send data via routing without showing in the URL. 通过使用此功能,我们可以通过路由发送数据而无需在URL中显示。

Plunker - working example of the same 柱塞-相同的工作示例

for more info read this awesome artical 了解更多信息,请阅读这篇很棒的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM