I tried to pass the data from one component to another via Subject through services,and i received the following error
Property 'emit' does not exist on type 'Subject(any)'.
so here is what i tried
component.ts file
import { Component, OnInit } from '@angular/core';
import { Productservice } from 'src/app/services/products.service';
import { Router, ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-admin-products',
templateUrl: './admin-products.component.html',
styleUrls: ['./admin-products.component.scss']
})
export class AdminProductsComponent implements OnInit {
listofproducts
editedproduct
constructor(private prservice:Productservice,private 
router:Router,private route:ActivatedRoute) { 
}
ngOnInit() {
this.listofproducts=this.prservice.getallproducts()
}
onclickedit(id){
this.editedproduct=this.prservice.getspecificproduct(id)
this.prservice.editproduct.emit(this.editedproduct)
this.router.navigate(['edit',id],{relativeTo:this.route})
}
}
so here in emit it says Property 'emit' does not exist on type 'Subject(any)
service file
import { Subject } from "rxjs";
import { Items } from "../home/header/admin-products/items.modal";
export class Productservice{
productcard=new Subject<any>()
editproduct=new Subject<any>()
getallproducts(){
    return this.cards
}
getspecificproduct(id){
    return this.cards[id]
}
} 
Here is the component where i subscribed it
import { Component, OnInit, Input } from '@angular/core';
import { Productservice } from 'src/app/services/products.service';
import { Router, ActivatedRoute } from '@angular/router';
import { take } from 'rxjs/operators';
@Component({
selector: 'app-edit-products',
templateUrl: './edit-products.component.html',
styleUrls: ['./edit-products.component.scss']
})
export class EditProductsComponent implements OnInit {
id:number
constructor(private prservice:Productservice,private 
router:Router,private route:ActivatedRoute) { }
ngOnInit() {
this.route.params.subscribe(
  (params)=>{
    this.id=+params['id']
    console.log(this.id)
  }
)
this.prservice.editproduct.pipe(take(1)).subscribe(
  (editproductdetails)=>{
    console.log(editproductdetails)
  }
)
}
}
You should emit values via next not emit.
Change this line:
this.prservice.editproduct.emit(this.editedproduct)
to this
this.prservice.editproduct.next(this.editedproduct)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With