【发布时间】:2016-10-07 00:57:13
【问题描述】:
我有一个 Angular 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);
}
}
它是我的另一个组件和subscibeCheckUser;
***********************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 请求,这意味着当用户登录时,我收到了对服务器的多个请求!!!我无法修复它...我不知道这个问题是否与 angular 2 有关,有人有这个问题吗?
最近几天我参与了这个问题!
【问题讨论】:
-
同stackoverflow.com/questions/37654458/…。请提供 Plunker 进行复制。
标签: javascript angularjs typescript angular rxjs