javascript - 如何链接订阅
问题描述
嗨,我正在尝试链接以下订阅。
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?
解决方案
我想你想要的是链接订阅时所做的操作。
你可以通过
bottemsheetRef.instance.change.pipe(
switchmap(resultFromChange => bottomSheetRef.afterDismissed
).subsbribe(resultFromAfterDismissed => {// do whatever you like})
推荐阅读
- python - ModuleNotFoundError:没有名为“_sysconfigdata_m_darwin_darwin”的模块
- perforce - 在与取消搁置存在冲突后,perforce 是否会安排解决?
- r - 在R中对具有指定逻辑的列进行分组
- reactjs - 尝试使用 react-redux 调度操作时,“PropsWithChildren”类型上不存在 Prop
- python - 用户输入表单字段后如何运行python函数
- python - 从数组中查找 3 个连续缺失的整数
- unit-testing - Vue、Mocha 和 webpack 块配置?
- flutter - 如何根据flutter中的if条件动态显示两个按钮
- tensorflow - 使用 Tensorflow 的 Dask - Joblib?
- arrays - Ruby:继续循环遍历一个固定长度的数组,每次迭代的每个元素都加 1