Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property emit does not exist on type

Tags:

angular

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)
  }
)

}

}
like image 203
Ratnabh Kumar Rai Avatar asked Oct 28 '25 21:10

Ratnabh Kumar Rai


1 Answers

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)
like image 59
MCMatan Avatar answered Oct 30 '25 16:10

MCMatan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!