Search code examples
angularangular-routingangular-router

Is it possible for a routed component to have inputs?


Let's say we have this component:

class MyComp {
  @Input() name!: string;
  constructor(){}
}

And this route definition:

{ path: 'path', component: MyComp }

How can I provide the name input to MyComp instance ?


Solution

  • If you want to pass data to routed component you can do it through route custom data:

    { path: 'path', component: MyComp, data: {name: 'myName'}}
    

    Then retreive it from ActivatedRoute

    constructor(private activatedRoute: ActivatedRoute) {}
    
    ngOnInit() {
      this.activatedRoute.data.subscribe(data => {
          this.name=data.name;
      })
    }