简体   繁体   English

如何手动运行父路由解析器Angular 2?

[英]How manual run parent route resolver Angular 2?

I have routes, one parent route and some children. 我有路线,一条父路线和一些孩子。 Parent router have Resolve. 父路由器有Resolve。

{path: ':orderId', component: OrderComponent,
    resolve: {order: OrderResolver},
    children: [...]}

All routes in "ngOnInit" get resolver data from Resolver determined for patent route: “ngOnInit”中的所有路线均获得专利路线确定的旋转变压器的旋转变压器数据:

let data = this.route.snapshot.pathFromRoot.reduce((pv, cv) => Object.assign(pv, cv.data), {});
this.order = data['order'];

In first child route I update ORDER and navigate to second child route. 在第一个孩子路线我更新ORDER并导航到第二个孩子路线。 But in second i get from resolver not-updater data['order']. 但在第二个我得到解析器not-updater数据['order']。 How I can run parent resolver from child route after update ORDER and in all sibling routes get actual data['order']? 如何在更新ORDER后在子路由中运行父解析器并在所有兄弟路由中获取实际数据['order']?

data will not be resolved again if you are just navigating from one child to other child of same parent. 如果您只是从一个孩子导航到同一父母的其他孩子,则不会再次解析数据。 It will only be resolved if you navigate away from your parent :orderId path. 只有在离开父:orderId路径时才能解决此问题。

What you may try is returning a Observable of order data, which you can set in sibling routes. 您可以尝试返回一个Observable订单数据,您可以在兄弟路线中设置。 and subscribe in child routes to get updated details. 并订阅子路线以获取更新的详细信息。 But know that when you navigate away from :orderId path and come back you will get a new Observable as the data will be resolved again. 但是要知道当你离开:orderId路径并返回时,你会得到一个新的Observable,因为数据将再次被解析。

Having said that , that is not a good pattern and I would recommend to use a service instead for data manipulation at a central place, if you know that you are going to update data among children. 话虽如此,这不是一个好的模式,如果你知道你要在孩子们之间更新数据,我建议在中心位置使用服务代替数据操作。

Hope this helps!! 希望这可以帮助!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM