【发布时间】:2019-10-30 16:13:00
【问题描述】:
我有 2 个订阅 - 一个是我的 ActivatedRoute 的订阅,另一个是来自 ngrx 商店的订阅。
ngOnInit() {
this.menuItems$ = this.store.select('menuItems');
this.menuItems$.subscribe(data => {
this.menuItems = data.menuItems;
});
}
ngAfterViewInit() {
this.fragmentSubscription = this.route.fragment.pipe(
filter((fragment: string) => typeof fragment === 'string')
).subscribe((fragment: string) => {
setTimeout(() => {
const element: ElementRef = this.menuItems.find(menuItem => menuItem.link === fragment).element;
if(element !== undefined) {
element.nativeElement.scrollIntoView({ behavior: "smooth", block: "start", inline: "center" });
}
});
});
}
由于我的 ActivatedRoute(片段)订阅取决于我的商店订阅数据,我想延迟我的 ActivatedRoute(片段)订阅,直到我的商店第一次订阅
这个有 rxjs 操作符吗?
Tnx
【问题讨论】:
-
你可以试试
forkJoinlearnrxjs.io/operators/combination/forkjoin.html -
@penleychan 看起来不错,但我只想这样做一次,而不是总是这样做。如果Store第一次发出已经完成,我不想再等ActivatedRoute订阅了
-
通过 router.navigate 方法中的 navigationExtras 更好地传递 menueItems 列表,从您导航到此页面的位置
标签: javascript angular typescript rxjs