简体   繁体   中英

How can I pass an object array from one component to another?

I have tried every way I've seen on internet, but it is impossible. When I write a console log on the other component, it says it's undefined.

(Its an object array btw)

This is from the first component:

  irAlCarrito():void{
let params = {queryParams: this.carrito};
this.router.navigate(['/carrito', params]);

}

This is from the second component:

  ngOnInit(): void {
this.router.queryParamMap.subscribe(params => this.carrito = params.getAll("carrito"));
console.log(this.carrito[0]);

}

three mistakes here

the first you must send string, not array or object for this we will make it JSON.stringify

the second, you must add a key for your value and not to pass it directly

the third, you queryParams should be send as a second param to navigate not as a second item in the routing array

here is the full working code

let params = {queryParams: {carrito: JSON.stringify(this.carrito)}};
this.router.navigate(['/carrito'], params);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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