Angular Passing Data Between Routes

后端 未结 3 587
傲寒
傲寒 2020-12-13 02:24

I have a little problem using routes in angular 4. You know, when I am trying to pass data from a component to another using navigate(\'root\', data), I just re

3条回答
  •  不知归路
    2020-12-13 03:04

    In the current version this is now available in @angular/router.

    Angular 7.2 introduces route state to NavigationExtras, which takes an object literal similar to queryParams, etc.

    The state can be set imperatively:

    this.router.navigate(['example'], { 
      state: { example: 'data' } 
    });
    

    or declaratively:

    
      Hello World
    
    

    And read in a top-level component using:

    this.router.getCurrentNavigation().extras.state;
    

    or within child components using:

    window.history.state
    

    Added a working example of it being used on StackBlitz

提交回复
热议问题