我有这样的场景:我将查询参数保存在服务中,以便在用户关闭浏览器时保持此数据的活动状态,使用解析器我使用来自活动路由的结果将缺少的参数添加到组件的Init挂钩中,在某些组件中,此查询参数涉及到执行UI或服务操作.
照顾好它:
export const queryParamsResolver = (route: ActivatedRouteSnapshot) => {
const productId = route.paramMap.get('id');
if (!productId) {
return of(undefined);
}
// is possible navigate from here instead of send the data the component and navigate from the componen
return inject(CodeService).getQueryParamerts(+productId);
};
在下列航线上航行:
ngOnInit() {
this.activatedRoute.data.subscribe(({ query }) => {
// extra navigation is not required only provide query params to the current url
this.router.navigate([], { queryParams: query });
});
}
可以使用更好的实现来处理某些组件的存储查询参数.我想知道是否可以在任何路由解析之前分配此参数,以帮助避免额外的呼叫,其他组件依赖于此查询来反映UI中的其他行为和未在解析器中请求的呼叫服务.
堆叠闪电战中的现场样本,没有全部的复杂性.