How to get all route params/data

前端 未结 6 574
执念已碎
执念已碎 2020-12-24 07:20

Given a route config

{
   path: \'/root/:rootId\',
   children: [{
       path: \'/child1/:child1Id\',
       children: [{
           path: \'/child2/:child2         


        
6条回答
  •  无人及你
    2020-12-24 08:13

    constructor(private route: ActivatedRoute) {}
    
    data$ = of(this.route).pipe(
        expand(route => of(route['parent'])),
        takeWhile(route => !!route['parent']),
        pluck('snapshot', 'data'),
        reduce(merge));
    

    Explanation:

    Calling expand here creates an observable for each route in the parent chain. Takewhile is used so that it stops recursing when route['parent'] returns null, or at the root route.

    Then, for all of those routes in the observable, pluck will map each to it's 'snapshot.data' property.

    Finally, reduce is being fed the merge function from lodash to combine all the data objects into one object. This stream aggregates all the data from the current route through the parent routes

提交回复
热议问题