我在角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来复制。 –