简体   繁体   English

如何将对象数组从一个组件传递到另一个组件?

[英]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.

相关问题 如何将对象从一个组件传递到另一个组件 - how can I pass an object from one component to another in angular 如何以角度从另一个组件传递对象? - How can i pass object one component from another component in angular? 如何将* ngIf值从一个组件传递到另一个组件? - How can I pass an *ngIf value from one component to another? 如何将数组传递给另一个组件? - How can I pass an array to another component? 如何在单击按钮时将 id 和对象从一个组件传递到另一个组件的 angular 函数? - How do I pass an id and object on click of a button from one component to another component' s function in angular? 如何从 Angular8 中的另一个组件传递一个组件的值(独立组件) - How can I pass value from one component from another component in Angular8 (Independent components) 如何将单击行的数据从一个组件传递到另一个组件而不传递到 url - How can i pass data of clicked row from one component to another component without passing to the url 如何在Angular 2中将对象从一个组件传递到另一个组件? - How to pass object from one component to another in Angular 2? 如何在Angular中将对象从一个组件传递到另一个组件 - How to pass an object from one Component to another in Angular 如何通过导航路线将对象从一个组件传递到另一个组件? - How to pass an object from one component to another via navigation route?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM