Hello friends, I have a doubt, it is possible to send data to service from one component and then from the service to send them to another component. in this code I have errors but in case what I would like is to pass the data array that is in the first component to the service and after the service sent it to component 2, and beforehand thanks for the help
component 1 <p>hola<p>
import { Component, OnInit } from '@angular/core'; import * as $ from 'jquery'; import {DatoscompraService} from './../datoscompra.service'
@Component({
selector: 'app-carteras',
templateUrl: './carteras.component.html',
styleUrls: ['./carteras.component.css'] })
export class CarterasComponent implements OnInit {
datos:any = [
{
producto:'Cartera 1',
cantidad: 3,
precio:200
},
{
producto:'Cartera 2',
cantidad: 1
, precio:500
},
];
constructor(private servicio:DatoscompraService) { }
ngOnInit() { }
public enviar(){
return this.datos;
}
}
service
import { Injectable } from '@angular/core';
@Injectable({ providedIn: 'root' }) export class DatoscompraService {
datoss:any[] = [];
constructor() { }
public enviarPara(){
}
}
component 2
import { Component, OnInit } from '@angular/core'; import * as $ from 'jquery'; import {DatoscompraService} from './../datoscompra.service'
@Component({
selector: 'app-cabecera',
templateUrl: './cabecera.component.html',
styleUrls: ['./cabecera.component.css'] })
export class CabeceraComponent implements OnInit {
datos:any[] = [];
constructor(private servicio:DatoscompraService){ this.datos = servicio.enviarPara(); }
ngOnInit() { }
}