我有一个更新我的数据库的服务我希望它更新我的祖父母和父组件。
我得到的行为是祖父组件正在从服务获取响应。如果我注释掉祖父母中的订阅代码,那么我会看到父行为。我想要的是每次更改 progressBar$ 的值时两个组件都做出响应
服务代码
export class CompetitionService {
progressBarSubject = new Subject<boolean>();
progressBar$ = this.progressBarSubject.asObservable();
private subscription: Subscription=new Subscription();
constructor(
private loginService: LoginService,
private http: Http
) { }
initializeCompetition(compDate: string) {
this.progressBarSubject.next(true);
const login = this.loginService.getLogin();
const url = AppSettings.API_ENDPOINT + "competition/addCompetition";
let headers = new Headers();
headers.append('C247Token', login.getToken())
let body = { 'C247Token': login.getToken(), 'compDate': compDate };
//execute http (must have a corisponding subscribe)
this.subscription = this.http.post(url, body, { headers: headers })
.subscribe(res => {
this.progressBarSubject.next(false);
},
err => {
this.handleError(err);
});
}
祖父组件
private isProgressBarOn: boolean = false;
private subscription: Subscription = new Subscription();
constructor(private loginService: LoginService,
private competitionService: CompetitionService,
private cd: ChangeDetectorRef,
private router:Router
) { }
ngOnInit() {
this.subscription = this.competitionService.progressBar$
.subscribe(
response => {
this.isProgressBarOn = response;
this.cd.detectChanges();
//alert("Stop "+'"'+this.isProgressBarOn+'"');
}
);//*/
}
父组件:
ngOnInit() {
this.initForm();
this.subscription = this.competitionService.progressBar$
.subscribe(response=>{
console.log('hit '+response);
}