how to send data between two child elements with angular 4

2

I have two three elements

  • Father
  • Son 1
  • Son 2

What I need is to be able to send an array of data from the element Son 1 to Son 2

<!-- Elemento Hijo 1 -->
<app-rango-form></app-rango-form>
<!-- Elemento Hijo 2 -->
<app-tablero-deudas-table></app-tablero-deudas-table>
    
asked by Jeffry Jose Barrios Gomez 15.05.2018 в 02:27
source

1 answer

0

If you only need to send data from one component to another, it would be best to create a service and inject it into both components.

You simply create a service.service.ts file, which contains the @Injectable decorator

import {Injectable} from '@angular/core';

@Injectable()

export class MiServicio {
// variable en la que puedes guardar informacion para acceder con el otro componente
    variableServicio: {};
}

You declare it as a provider in your module:

components: [MiComponente, ChildAComponent, ChildBComponent],
providers: [MiServicio] // aqui declaras todos los servicios que vayas a usar

In your component you import the service and inject it into your constructor:

import {MiSericio} from './servicio.service';

constructor(public servicio: MiServicio){}
// y accedes a la variable dentro del servicio de la siguiente forma
// tambien puedes usar observables y funciones en el servicio y llamarlos usando this.servicio
miVar = this.servicio.variableServicio;

Here I leave articles about the use of services
link
link

    
answered by 26.07.2018 в 21:30