Router getCurrentNavigation always returns null

后端 未结 3 1230
南旧
南旧 2020-12-24 11:00

In the latest version of Angular 7.2.6, I\'m trying to pass data in router itself

this.router.navigate([\'other\'], {state: {someData: \'qwert\'}}

相关标签:
3条回答
  • 2020-12-24 11:34

    change the code like this because after constructor() only the ngOnInit() gets called so the value is getting null

    constructor(private router: Router) {
       this.name = this.router.getCurrentNavigation().extras.state.example;
    }
    
    0 讨论(0)
  • 2020-12-24 11:40

    work for me Ionic 4

    • this.router.url => /app/menu/etc // current position
    0 讨论(0)
  • 2020-12-24 11:43

    You're calling the method getCurrentNavigation too late. The navigation has finished.

    You need call the getCurrentNavigation method inside of the constructor:

    constructor(private router: Router) {
        this.name = this.router.getCurrentNavigation().extras.state.example;
    }
    

    Or if you want to access the navigation in ngOnInit you can do following:

    ngOnInit() {
        this.name = history.state.example;
    }
    
    0 讨论(0)
提交回复
热议问题