简体   繁体   English

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

[英]how can I pass an object from one component to another in angular

I have a component with different modal and in each modal I must show the information of each property, this information is in another component. 我有一个具有不同模态的组件,在每个模态中,我必须显示每个属性的信息,此信息在另一个组件中。 I have that information in a vector of objects. 我在对象向量中拥有该信息。 From what I read I must use Binding between components (father and son) the doubt that I have is how is an object that I must pass to the other component and then show their data, how could I do it? 从我阅读的内容中,我必须在组件(父子)之间使用绑定,我所怀疑的是如何将一个对象传递给其他组件然后显示其数据,我该怎么做? I must declare an object type real @input and how would you do to bindear the whole object from one component to another? 我必须声明一个对象类型为real @input,并且如何将整个对象从一个组件绑定到另一个组件? what I want to do is show the values of that object in the template of that component. 我要做的是在该组件的模板中显示该对象的值。

Here is a sample of what I have: 这是我所拥有的样本:

I have this class that has been the object of which I speak: 我上过这个课,我一直在讲:

 export class Inmueble { nombre: string=""; id: string=""; baño: number=0; estacionamiento: number=0; metro: number=0; precio: number=0; fotos: string[]=[]; } 

I import that class into COMPONENT1 where I fill it with data through a request to an API. 我将该类导入COMPONENT1,在其中通过对API的请求向其中填充数据。 What I want is (after filling that object) to pass it to the COMPONENT2, what I was thinking was to do this in COMPONENT2: 我想要的是(在填充该对象之后)将其传递给COMPONENT2,我当时想的是在COMPONENT2中执行此操作:

 import { Component, OnInit, Input } from '@angular/core'; import { Inmueble } from '../modelos/inmueble'; class { @Input vector: inmueble[]; } 

but how do I do for the object filled from COMPONENT1 to COMPONENT2? 但是如何处理从COMPONENT1到COMPONENT2填充的对象? in the examples I see that they do it for a single variable like this: [variable] = "value", but this would be a vector and I think that doing it line by line would be very unimportant. 在示例中,我看到它们是针对单个变量执行的,如下所示:[variable] =“ value”,但这将是一个向量,我认为逐行执行此操作并不重要。 What I want to achieve with this is to show that object in the COMPONENT2 template. 我要实现的目的是在COMPONENT2模板中显示该对象。 Thanks to everyone beforehand. 预先感谢大家。 Regards! 问候!

Edit: 编辑:

Component 1: 组件1:

 import { Component, OnInit, Input} from '@angular/core'; import { Inmueble } from '../modelos/inmueble'; @Component({ selector: 'app-modal', templateUrl: `<p>Model: {{prueba.nombre}} </p>`, styleUrls: ['./modal.component.css'] }) export class ModalComponent implements OnInit, Input { @Input() prueba: Inmueble; constructor() { } ngOnInit() { } } 

Component 2: 组件2:

  <app-modal [prueba]="inmuebles[0]" > </app-modal> 

I assign real estate [0] since in this component I have an array of properties, so in theory I would be assigning a property to a property. 我分配不动产[0],因为在此组件中我拥有一系列属性,因此从理论上讲,我会将一个属性分配给一个属性。

is showing me this error: 向我显示此错误:

 ERROR in ./src/app/modal/modal.component.ts Module not found: Error: Can't resolve './<p>Model: {{prueba.nombre}} </p>' in '/home/julian/Seaconfiable/src/app/modal' 

If I have understood it correctly, you need to pass complex data object across components. 如果我正确理解它,则需要跨组件传递复杂的数据对象。

You can easily do this using property binding from parent component to child component as an @Input property as 您可以使用父组件到子组件的property binding作为@Input属性,轻松地完成此操作

 @Input() vector: inmueble[];

A sample created at https://stackblitz.com/edit/angular-object-passing-between-components https://stackblitz.com/edit/angular-object-passing-between-components创建的示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以角度从另一个组件传递对象? - How can i pass object one component from another component in angular? 如何将对象数组从一个组件传递到另一个组件? - How can I pass an object array from one component to another? 如何在单击按钮时将 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) 如何在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 如何在 Angular 中将数据从一个组件传递到另一个组件? - How do I pass data from one component to another in Angular? 如何使用 Angular 8 将按钮单击时的数据从一个组件传递到另一个组件? - How can I pass data on button click from one component to another using Angular 8? 如何将json对象的一个​​组件传递给angular中的另一个组件? - How to pass json object one component to another component in angular? 如何将* ngIf值从一个组件传递到另一个组件? - How can I pass an *ngIf value from one component to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM