Parent components gets empty Params from ActivatedRoute

前端 未结 5 1414
小鲜肉
小鲜肉 2020-12-08 14:51

I have a main component that has a router-outlet in it. In the component that is loaded in the router-outlet I grab the url parameter like this:

ngOnInit():         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 15:24

    I was having a very similar problem with this mainly due to my misunderstanding of how routes actually worked. I thought I could just go up the chain of path parameters and each one would be a parent/child relationship. However, if a route has more than one element to the path, (e.g. /dashboard and profile/:user_id) it will depend on how you set up the routes in your routing module(s).

    To explain a different way that made it click for me:

    // If I am trying to access ActivatedRoute from the CheckMobileComponent (say 
    // through a `router-outlet`), I could get it through something like 
    // `this._route.firstChild`
    
    { path: '', component: CheckMobileComponent, children: [
    
        // If I want to get things from the '' context in MobileReportComponent, it 
        // would be through something like `this._route.parent`
    
        { path: 'report', component: MobileReportComponent }
    
      ]   
    
    }
    

提交回复
热议问题