How to pass component data to other components [duplicate]

0

I am developing a project in angular, in which I upload a file in json formats and show it in a table. Until now I have three components that are:

  • UploadComponent
  • TableComponent
  • TableDetalleComponent

My question is how from UploadComponent I can send the data to TableComponent and TableDetalleComponent, this is my code:

import { Component, OnInit } from '@angular/core';
import { TableComponent } from '../table/table.component';
import { MatPaginator, MatSort, MatTableDataSource} from '@angular/material';
import {tableInf} from './upload.model';
import {UploadService} from '../services/upload.service';

@Component({
  selector: 'app-upload',
  templateUrl: './upload.component.html',
  styleUrls: ['./upload.component.css']
})



export class UploadComponent implements OnInit {

displayedColumns: string[] = ['noMaquina', 'cantidadDatos','promedioTCH', 'tiempoTrabajo', 'promedioVelocidad', 'noDescargas' ];
fileText;
datos;
datosServices;
nombre:any="";
mee=this;

fileUpload(event) {
   var reader = new FileReader();
   reader.readAsText(event.srcElement.files[0]);
   var me = this;
   reader.onload = function () {
     me.fileText = reader.result;
     var json = me.fileText;
     me.datos = JSON.parse(json)
     me.nombre=me.datos;
   }
   this.pruebas();
 }

 public pruebas(){
   alert("Mis datos: " + this.nombre.id);
   debugger;
 }


constructor() {}

  ngOnInit() {
  }
}

Please can you give me a help !!

    
asked by Dany Ladino 24.07.2018 в 21:38
source

1 answer

0

The easiest way to do this is using services.

You can generate them directly with angular cli ng generate service MiServicio

Then you create a variable or some object of public scope (In the service)

Then in the constructor of your components, you refer to the name of your service

constructor(private miServicio: MiServicio) { }

There you already have your data accessible from any component where you import your service.

Here is step by step, any doubt you can make it known in the comments link

    
answered by 25.07.2018 в 06:50