Angular2 Get router params outside of router-outlet

后端 未结 5 1471
不思量自难忘°
不思量自难忘° 2021-01-04 14:26

I have a dashboard application which consists of a treeview component (which lists various content nodes) and a dashboard-edit component which renders some editable content

5条回答
  •  青春惊慌失措
    2021-01-04 14:45

    Is it only possible to access route params in a component rendered by a router-outlet?

    Yes, the tells Angular2 to treat the containing component as a "routing" component. Therefore you cannot get a RouteParams instance injected into the class as it wasn't instantiated via the routing directive.

    If not, then what am I doing wrong?

    I wouldn't say you're doing anything wrong, you simply had a misconception on how it was designed. I too has this initial misconception. I found this Angular2 article to be a great source for understanding how to pass data around and how to communicate betwixt parent and child components.


    In your specific case I'd suggest removing the RouteParams from the constructor of the ContentTreeComponent as it will only be available if rendered from a "routing" component.

    export class ContentTreeComponent implements OnInit {
    
        constructor(
            private _contentService: ContentService,
            private _router: Router
        ) { }
    
        // Omitted for brevity...
    }
    

    Then in order to get the id, you'd probably have to share a bit more of your top-level code so that I can see where it is coming from...

提交回复
热议问题