通过路由angular2在RouteParams中传递的对象/数组 [英] Object/array passing in the RouteParams via Routing angular2

查看:414
本文介绍了通过路由angular2在RouteParams中传递的对象/数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在angular2中进行路由.实际上我知道我们可以在路由中使用RouteParams发送键/值,但是我想在RouteParams中发送整个对象/数组,我不知道是否可能.有没有办法做到这一点呢?

Working on Routing in angular2. actually i know we can send key/value using RouteParams in the routing but i want to send whole object/array in the RouteParams, i don't know is it possible or not. is there any way to do so if not then any alternate who knows ?

我当前的HTML部分是:

my current HTML part is :

<div *ngFor="#books of getBooks_array #i=index">
    <a [routerLink]="['/IssueBook', {name: books.book_name, isbn: books.book_ISBN,author: books.book_author, price: books.book_price, id: books.book_id, language: books.book_language}]"></a>
</div>

我当前的.ts康乃馨:

my current .ts conatins:

constructor(@Inject(RouteParams) params:RouteParams, @Inject(Http) http:Http){
        this.name= params.get('name');
        this.id= params.get('id');
        this.language= params.get("language");
        this.price= params.get("price");
        this.author= params.get("price");
        this.isbn= params.get("isbn");
}

但是我想发送RouteParams:

but i want to send RouteParams like this :

<div *ngFor="#books of getBooks_array #i=index">
<a [routerLink]="['/IssueBook', {data: books}]">Books</a>
</div>

我正在尝试获取这样的数据:

and i am trying to get data like this:

constructor(@Inject(RouteParams) params:RouteParams, @Inject(Http) http:Http){
        this.data= params.get('data');
}

但是我遇到了错误:

推荐答案

您可以将对象传递给Angular2中的Url,但实际上并不受支持. routeParams上的get-Method仅返回字符串表示形式,因此您将获得类似于"[Object]"的字符串. 如果要访问传递的数据,则需要使用params-Property:

You can pass Objects to Url's in Angular2, but it's not really supported. The get-Method on routeParams only returns a string-representation, so you would get a string like "[Object]". If you want to access the passed data, you need to use the params-Property:

constructor(@Inject(RouteParams) params:RouteParams, @Inject(Http) http:Http){
    this.data= params.params.data;
}

但是请注意,这仅在您首次拨打路线时才可以正常使用. 如果您使用不同的数据再次调用该路由,则可能会发生params.params.data返回旧数据的情况,尽管您将新数据放入了该路由.

But beware, this only works properly the first time you call the route. If you call the route anothertime with different data, it can happen that params.params.data returns your old data although you put new data into the route.

这篇关于通过路由angular2在RouteParams中传递的对象/数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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