Parent components gets empty Params from ActivatedRoute

前端 未结 5 1421
小鲜肉
小鲜肉 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:19

    ActivatedRoute: Contains the information about a route associated with a component loaded in an router outlet. If you would like to access route details outside of it, use the code below.

    import { Component } from '@angular/core';
    import { Router, ActivatedRoute, Params, RoutesRecognized } from '@angular/router';
    
    export class AppComponent {
    
        constructor(private route: ActivatedRoute, private router: Router) {
    
        }
    
        ngOnInit(): void {
            this.router.events.subscribe(val => {
    
                if (val instanceof RoutesRecognized) {
    
                    console.log(val.state.root.firstChild.params);
    
                }
            });
    
        }
    }
    

    There are other ways to share data in between components for example by using a service.

    For more details about how you can tackle this as concept, read comments here.

提交回复
热议问题