[英]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
首先您必须发送字符串,而不是数组或对象,我们将其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第三,您的 queryParams 应该作为第二个参数发送以导航而不是作为路由数组中的第二项
here is the full working code这是完整的工作代码
let params = {queryParams: {carrito: JSON.stringify(this.carrito)}};
this.router.navigate(['/carrito'], params);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.