我想将一些数据从一个模块传递到另一个模块.为此,我使用主语.我创建了以下主题:
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class SelectedClusterService {
constructor() { }
public selectedClusterName = new Subject<string>();
}
我从"oneComponent"通过上面的服务传递一些数据,然后重定向到"anotherComponent":
this.selectedClusterService.selectedClusterName.next(cluster_name);
并将数据提取到"另一个组件",如下所示:
this.selectedClusterService.selectedClusterName.subscribe(res => {
this.selectedCluster = res;
console.log("this.selectedCluster", this.selectedCluster);
});
这工作得很好,我正在将数据发送给"另一个组件".但在"另一个组件"中,我使用的是forkJoin.我只想在从forkJoin获得数据之后才从Subject获得数据. 我试着这样做:
ngOnInit() {
forkJoin([
this.dataService.getUpgradeAppList(),
this.dataService.getClusterList()
]).subscribe((data: any) => {
console.log("forkJoin :: ngOnInit ::", data);
this.appList = data[0].apps;
this.clusterList = data[1];
});
this.selectedClusterService.selectedClusterName.subscribe(res => {
this.selectedCluster = res;
console.log("this.selectedCluster", this.selectedCluster);
});
}
我已经try 了ngAfterViewInit
、ngAfterContentChecked
和changeDetect,我也try 使用setTimeout
,但都不起作用.我总是先获取主题数据,然后再获取forkJoin数据.
如何才能先获取forkJoin,然后再获取主题数据?
任何帮助我们都会很感激.