angular - Angular 8/9 -(动态订阅):如何在从集合中获取 Observables 时自动 forkJoin?
问题描述
第一步:
想象一下,在项目的某处有一个对象RouteObservables
,我想在许多组件中使用(导入)它:
export const RouteObservables = {
state$: 'this.route.paramMap.pipe(map(() => window.history.state))',
url$: 'this.route.url',
params$: 'this.route.params',
queryParams$: 'this.route.queryParams',
fragment$: 'this.route.fragment',
data$: ' this.route.data'
};
第二步:
我想达到以下情况(通过使用上面的 RouteObservables 对象):
const state$ = this.route.paramMap.pipe(map(() => window.history.state));
const url$ = this.route.url;
const params$ = this.route.params;
const queryParams$ = this.route.queryParams;
const fragment$ = this.route.fragment;
const data$ = this.route.data;
第三步
我想使用相同的集合RouteObservables
来forkJoin
自动启动:
forkJoin([...Object.keys(RouteObservables)]).subscribe(
(routeData: any) => {
this.routeData = routeData;
}
);
为什么我需要这个RouteObservables
对象?
我需要有序序列来访问正确的数据(例如,routeData[0]
将是我的状态对象可能从前一个路由转移)。这个对象也可以帮助我不要错过最后取消订阅的一些订阅(ngOnDestroy
)。
我的问题:
什么是最有效的方法,在对象(或集合)中以序列(我感兴趣)声明 Observable,以便我动态地执行一些操作?
如果没有经过验证的(最先进的)方法,我怎样才能从第一步到第二步?
我可以完全省掉第二步,更优雅地进入第三步吗?
编辑1:
顺便说一句:forkJoin
不能以这种方式与路由 (ActivatedRoute) 可观察对象一起使用,因为路由可观察对象不完整。以下方法有效,但我的问题仍然存在:
// excerpt!
// routeData is a public property
ngOnInit() {
this.getAllRouteData();
}
getAllRouteData() {
const state$ = this.route.paramMap.pipe(map(() => window.history.state));
const url$ = this.route.url;
const params$ = this.route.params;
const queryParams$ = this.route.queryParams;
const fragment$ = this.route.fragment;
const data$ = this.route.data;
forkJoin(
state$.pipe(first()),
url$.pipe(first()),
params$.pipe(first()),
queryParams$.pipe(first()),
fragment$.pipe(first()),
data$.pipe(first())
).subscribe(
(routeData: any) => {
this.routeData = routeData;
this.start();
},
(error: any) => {
console.log('Error: ', error);
}
);
}
Edit2:当前解决方法(routeObservables
在其他组件中不能真正重用)
getAllRouteData() {
const routeObservables = [
this.route.paramMap.pipe(map(() => window.history.state)),
this.route.url,
this.route.params,
this.route.queryParams,
this.route.fragment,
this.route.data
];
forkJoin(routeObservables.map(r => r.pipe(first()))).subscribe(
(routeData: any) => {
this.routeData = routeData;
}
);
}
我为什么要问?
我最大的问题是“单一责任”,我试图避免复制+粘贴代码(例如routeObservables
,在每个组件中,需要路由相关数据)。
解决方案
所以主要问题是你有字符串值并且需要将它们用作 Javascript 对象,对吗?在这种情况下,您可以使用方括号访问这些属性:
const keys = Object.keys(this.RouteObservables);
const sources = this.keys.map(key => this[this.RouteObservables.key]);
forkJoin(...this.sources).subscribe(
(routeData: any) => {
this.routeData = routeData;
}
);
ps 类必须this.route
在其构造函数中注入。
尚未测试,但我认为这应该可以解决问题。如果有任何错误,请告诉我。
推荐阅读
- apache-spark-sql - 无法通过从下拉选择转换的选定字段代码使用 SQL 访问全局视图表
- javascript - 如何检查网页是否可以在 iframe 中加载
- javascript - 我如何重新渲染我的 vue 组件(模态)
- python - JSON格式不对
- java - 如何在 jpql 和 hql 中使用 MySQL 日期函数
- jquery - Ajax 自动完成从数据库中填充搜索框
- jfrog-cli - 执行 jfrog 命令时,简单终端(ST)退出但没有错误消息
- python - 没有否定前缀
- javascript - 为什么 react 将字符串数组渲染到 HTML 元素中?
- android - Android:关于从睡眠中唤醒 CPU、执行任务然后返回睡眠的方法的建议