How do I pass data to Angular routed components?

前端 未结 16 1345
挽巷
挽巷 2020-11-22 08:03

In one of my Angular 2 routes\'s templates (FirstComponent) I have a button

first.component.html

16条回答
  •  暖寄归人
    2020-11-22 08:41

    It is 2019 and many of the answers here would work, depending on what you want to do. If you want to pass in some internal state not visible in URL (params, query) you can use state since 7.2 (as I have learned just today :) ).

    From the blog (credits Tomasz Kula) - you navigate to route....

    ...from ts: this.router.navigateByUrl('/details', { state: { hello: 'world' } });

    ...from HTML template: Go

    And to pick it up in the target component:

    constructor(public activatedRoute: ActivatedRoute) {}
    
      ngOnInit() {
        this.state$ = this.activatedRoute.paramMap
          .pipe(map(() => window.history.state))
      }
    

    Late, but hope this helps someone with recent Angular.

提交回复
热议问题