[英]Angular 2 router evaluating multiple route parameters
In my app I have routes with multiple parameters, eg在我的应用程序中,我有多个参数的路由,例如
http://localhost:3000/department/6/employee/45/sales?from=20160921&to=20160928
How do I best approach evaluating these parameters?我如何最好地评估这些参数? I can subscribe to
params
and queryParams
of the ActivatedRoute
,我可以订阅
ActivatedRoute
params
和queryParams
,
this.route.params.subscribe(params => ...load data for params);
this.route.queryParams.subscribe(queryParams=> ...load data for queryParams);
However, this will trigger load twice, of course.但是,这当然会触发加载两次。 Also, I always need both pieces of information, for instance
sales
and from
/ to
from the above example.另外,我总是需要这两条信息,例如上面的例子中的
sales
和from
/ to
。
So the above code becomes所以上面的代码变成
this.route.params.subscribe(params => {
let queryParams = this.route.snapshot.queryParams;
...load data for params/queryParams
}
this.route.queryParams.subscribe(queryParams => {
let params = this.route.snapshot.params;
...load data for params/queryParams
}
And I still haven't got a solution to the problem of triggering load twice.而且我还没有解决两次触发加载的问题。
To make matters worse, I also need to figure out just how many levels I need to go up in my ActivatedRoute
, so it's not unusual to see更糟糕的是,我还需要弄清楚我的
ActivatedRoute
需要上升多少级,所以看到它并不罕见
this.route.parent.parent.parent.params.subscribe(...)
Ugh.啊。 And that breaks down for a lazy loaded component anyway.
无论如何,这会因延迟加载的组件而崩溃。 In any case, it's a lot of boilerplate code just to retrieve a handful of route parameters.
无论如何,只是为了检索少量路由参数需要大量样板代码。
Am I doing this wrong?我这样做错了吗? Is there a recommended way of going about this?
有没有推荐的方法来解决这个问题?
You may try below,你可以试试下面,
paramSubscription = Observable.forkJoin(
this.route.params,
this.route.queryParams,
this.route.parent.parent.parent.params
).subscribe(res => {
this.combinedParams = { params :res[0], queryParams :res[1], ancestorParam : res[2] };
// Load data
});
// unsubscribe from the subscription
ngOnDestroy = () => {
this.paramSubscription.unsubscribe();
}
Please note I have not tested this code yet.请注意,我还没有测试过这段代码。
Hope this helps!!希望这可以帮助!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.