首页 > 解决方案 > 如何链接订阅

问题描述

嗨,我正在尝试链接以下订阅。

  changeBranch() {
const bottomSheetRef: MatBottomSheetRef = this.bottomSheet.open(CCSBranchComponent, {
  data: this.branches
});
this.subscription.add(bottomSheetRef.instance.change.subscribe((branch: Branch) => {
  this.branchInfo = `Description : ${branch.author}\nAuthor : ${branch.id}\nCreated date :${branch.created}`;
  this.blockpointBranchForm.get('branch').setValue(branch.id);
}));
this.subscription.add(bottomSheetRef.afterDismissed().subscribe(() => {
this.returnSelectedBranch.emit(this.blockpointBranchForm.get('branch').value);
}));

}

在这里,如果在工作表加载之前调用了 bottomSheetRef.instance.change.subscribe,它会抛出 undefined。所以我正在尝试实现看起来像这样的东西

this.subscription.add(this.bottomSheet.open(CCSBranchComponent, {
  data: this.branches
}).instance.change.subscribe((branch: Branch) => {
  this.branchInfo = `Description : ${branch.author}\nAuthor : ${branch.id}\nCreated date :${branch.created}`;
  this.blockpointBranchForm.get('branch').setValue(branch.id);
}).afterDismissed().subscribe(() => {
this.returnSelectedBranch.emit(this.blockpointBranchForm.get('branch').value);
}));

这里第二个订阅在订阅返回时被调用。如何访问链中的 observable?

标签: javascriptangularrxjsangular-material

解决方案


我想你想要的是链接订阅时所做的操作。

你可以通过

bottemsheetRef.instance.change.pipe(
 switchmap(resultFromChange => bottomSheetRef.afterDismissed
).subsbribe(resultFromAfterDismissed => {// do whatever you like})

推荐阅读