Can Angular 2 pass a complex object along a router?

Is it possible to send a complex object through a router? Here is what I am doing and trying to do:

On the search page, use can click a button on one of the results, which calls the method that calls this line. this.router.navigate(['profile-detail', selection]);

The object selectionlooks the same as before moving.

{
  profile: {
    id: number,
    fname: string,
    lname: string
  },
  contact: {
    type: string,
    address: string
  },
  books: [{
    title: string,
    author: string
  }]
}

However, when we go to the profile details page, it this.route.snapshot.paramshas this as actual data:

{
  profile: "[object Object]",
  contact: "[object Object]",
  books: "[object Object]"
}
+4
source share
2 answers

No, the router must serialize the data in the browser URL string, and the URL string only supports the string.

, / , .

. https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service

https://angular.io/docs/ts/latest/guide/router.html#!#resolve-guard

+3

JSON.stringify

0

Source: https://habr.com/ru/post/1666788/


All Articles