简体   繁体   English

Angular 2路由:父处理参数

[英]Angular 2 Routing: Parent Handling Parameter

I'm running into an error with my Angular 2 Routing. 我的Angular 2路由遇到错误。 (Using angular/router2.0.0-rc1) (使用angular / router2.0.0-rc1)

Heres what the routes look like: 这是路线的样子:

tabs/editItem (For creating a new item)
tabs/id/editItem (For editing an item)
tabs/id/itemStuff
tabs/id/differentStuff

On my tabs component, I need to disable the itemStuff and differentStuff options when no ID is given, but enable when there is an id. 在我的tabs组件上,当没有给出ID时,我需要禁用itemStuff和differentStuff选项,但在有ID时启用。

I used NgIf on the ID in my template and: 我在模板的ID中使用了NgIf,并且:

  routerOnActivate(curr: RouteSegment) {
    if(curr.getParam('pId') == null)
      return;

    this.pId = +curr.getParam('pId');
  }

The problem is that I cannot access the routes parameters from my Tabs page because "routerOnActivate" is only called at the routes endpoint. 问题是我无法从“选项卡”页面访问路由参数,因为“ routerOnActivate”仅在路由端点被调用。 It appears an option is to have the children elements do the check for me and then send an event back for the Tabs component to update, but that seems ugly and not the right way to do this. 似乎可以选择让子元素为我做检查,然后将事件发送回给Tabs组件进行更新,但这似乎很丑陋,而且不是正确的方法。

Any help is appreciated! 任何帮助表示赞赏!

TLDR: How can a parent component access and handle a parameter TLDR:父组件如何访问和处理参数

Firstly, its likely best that you upgrade your project so that you're using v3 of the router. 首先,最好升级您的项目,以便使用路由器的v3。 The v2 router is no longer being developed and the v3 router works quite differently to the v2 router. 不再开发v2路由器,并且v3路由器的工作方式与v2路由器完全不同。

As for your parent/child parameter issue, unfortunately you'll have a bit of the same problem of v3 in that children routes are not given the parents parameters. 至于您的父/子参数问题,不幸的是,您会遇到v3的相同问题,因为没有为子路由提供父参数。 They are delivered via a new injectable router component ActivatedRoute which only holds the parameters for the given route, not its parent or children. 它们通过新的可注射路由器组件ActivatedRoute传递,该组件仅保存给定路由的参数,而不保存其父级或子级的参数。

This behaviour appears to be by design which means that you will need to handle parameters in the components that 'own' that parameter, and then make these available via a seperate service to your other parent / child routes. 此行为似乎是设计使然,这意味着您将需要处理“拥有”该参数的组件中的参数,然后通过单独的服务将其提供给其他父/子路由。

As for a workaround, I haven't yet seen any ways of doing this directly through the routers components, but I'd say that with the constant changes to the routers API, workarounds are likely not a stable option regardless. 至于解决方法,我还没有看到任何直接通过路由器组件执行此操作的方法,但是我想说,随着路由器API的不断变化,解决方法无论如何都不是一个稳定的选择。

I guess that while this seems like this behaviour might be the wrong way of doing things and more difficult, its a strong separation of concerns which on larger projects is going to make your project more robust in the long run. 我猜想,虽然这种行为似乎是错误的处理方式,而且难度更大,但从长远来看,大型项目将使您的项目更加健壮。

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

Resurrecting this since I just ran into this issue. 因为我刚遇到这个问题,所以要复活这个。 You should be able to grab properties from the parent by climbing up the ActivatedRoute tree. 通过爬上ActivatedRoute树,您应该能够从父级那里获取属性。

  getId(route: ActivatedRouteSnapshot){
      let id = route.params["id"];
      if(id)
      {
        return id;
      }
      if(route.parent)
      {
        return this.getId(route.parent)
      }
      return undefined;
  }

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

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