我在角2 ShareService工作兩次或,角2訂閱shareService幾次
******************************shareService*************************
import { BehaviorSubject , Subject} from 'rxjs/Rx';
import { Injectable } from '@angular/core';
@Injectable()
export class shareService {
isLogin$:BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
CheckUser = this.isLogin$.asObservable();
public isLogin (bool){
this.isLogin$.next(bool);
}
}
及其我的另一組件和subscibe
的CheckUser
;
***********************another Component*******************************
_shareService.CheckUser.subscribe((val) =>{
*********all of this scope execute for several times just i have one another component and one next function*******
this.isLogin = val;
alert(val);
if(this.isLogin){
console.log("req req req");
this.MyBasket();
}
else if(this.ext.CheckLocalStorage("ShopItems")){
this.ShopItems = JSON.parse(localStorage.getItem("ShopItems"));
setTimeout(() => {
_shareService.sendShopItems(this.ShopItems);
},100);
}
});
我的問題是我執行一次this.isLogin$.next(bool)
但訂閱功能執行兩次或多次!!!!我的籃子功能是一個xhr請求,這意味着當用戶在我得到多個請求到服務器!!!我無法修復它...我不知道這個問題是爲角2或沒有,任何人都有這個問題? 過去幾天我參與了這個問題!
相同在http://stackoverflow.com/questions/37654458/shareservice-in-angular-2-twice-subscribe-the-next-function。請提供一個Plunker來複制。 –