Get route parameters in component

后端 未结 2 1092
离开以前
离开以前 2020-12-19 09:15

I am working on the Angular-6 project. I have many child routes for my application. One of them is as follow:

  const routes: Routes = [
  {
    path: \'inn         


        
相关标签:
2条回答
  • 2020-12-19 09:46

    Use Activated Route. As index of the params array you can use any parameter name.

    constructor(private itunes:SearchService,
        private route: ActivatedRoute) {
    
        this.route.params.subscribe( params => params[<any parameter name>]); 
    }
    
    0 讨论(0)
  • 2020-12-19 09:49

    Create a service (let's call it RouteParamsService) :

    export class RouteParamsService {
      innovationTrackId = new BehaviorSubject(undefined);
    }
    

    In your parent component, subscribe to params :

    constructor(private route: ActivatedRoute, private service: RouteParamsService) {
      route.params
        .subscribe(params => service.innovationTrackId
          .next(params && params['innovation-track-id'] || undefined))
    }
    

    Now you can subscribe to your service in any component, and you will get the value of your param. The parent component will handle any value change, and the service will propagate the change to any component that subscribed to it.

    0 讨论(0)
提交回复
热议问题