I have a ShareService in angular 2,and in another component i subscribe that,and this is my code :
*******ShareService ***********************************
private ShopItem$ = new Subject<any>();
ShopItem$_ = this.ShopItem$.asObservable();
public addToCart(item : any){
this.ShopItem$.next(item);
}
*************in another component**********************
_shareService.ShopItem$_.subscribe((item) => {
alert("hiii"); <====>this functions execute twice
this.ADD_TO_CART(item);<====>this functions execute twice
});
my problem is ADD_TO_CART() function execute twice!why?i dont want my cart function execute twice,how to fix this problem?? the ShareService dont have any problem i test it with an function and once execute.
See Question&Answers more detail:os