Angular无法获取路由的@inputData - getCurrentNavigation()方法返回null值。
创始人
2024-10-30 01:00:54
0次

在Angular中,getCurrentNavigation()方法用于获取当前路由的导航状态。然而,在某些情况下,该方法可能返回null值,这可能是由于异步导航或刷新页面等原因造成的。以下是解决此问题的一种常见方法:

  1. 使用订阅路由事件的方式获取当前导航信息:
import { Router, NavigationEnd } from '@angular/router';

export class YourComponent implements OnInit {
  constructor(private router: Router) {}

  ngOnInit() {
    this.router.events.subscribe(event => {
      if (event instanceof NavigationEnd) {
        const navigation = this.router.getCurrentNavigation();
        if (navigation) {
          const data = navigation.extras.state;
          // 使用获取到的data进行后续操作
        }
      }
    });
  }
}

在上面的代码中,我们订阅了路由事件,并在每次导航结束时检查getCurrentNavigation()方法的返回值。如果返回值不为null,我们可以从导航对象的extras属性中获取数据。

  1. 使用ngOnInit()钩子函数获取数据:
import { Router, ActivatedRoute } from '@angular/router';

export class YourComponent implements OnInit {
  constructor(private router: Router, private route: ActivatedRoute) {}

  ngOnInit() {
    const navigation = this.router.getCurrentNavigation();
    const data = navigation?.extras?.state;
    // 使用获取到的data进行后续操作
  }
}

在上面的代码中,我们通过注入ActivatedRoute来获取当前路由的导航信息。然后,我们可以使用getCurrentNavigation()方法来获取数据。

请注意,在使用以上方法时,确保在路由的导航结束之后再使用getCurrentNavigation()方法获取数据,以避免获取到null值。

相关内容

热门资讯

WPK模拟器法门!wepoke... WPK模拟器法门!wepoker手机版透视脚本,德州微扑克一直真的有辅助脚本1、打开软件启动之后找到...
WPK下载窍要!wepoker... WPK下载窍要!wepoker有辅助插件吗,德州微扑克真是真的是有辅助器工具1、WPK下载窍要!we...
WPK方法指南书!wepoke... WPK方法指南书!wepoker有没有挂,德州微扑克原来是真的有辅助插件1、完成wepoker有没有...
WPK脚本学习!wejoker... WPK脚本学习!wejoker透视方法,德州微扑克其实存在有辅助器app1、这是跨平台的wejoke...
WPK购买绝活!wepoker... WPK购买绝活!wepoker透视脚本免费app,德州微扑克都是存在有辅助器工具1、玩家可以在wep...
WPK控制牌积累!wepoke... WPK控制牌积累!wepoker分析,德州微扑克切实真的是有开挂方法1、wepoker分析公共底牌简...
WPK下载窍门!wepoker... WPK下载窍门!wepoker辅助工具,德州微扑克一贯有开挂神器1、每一步都需要思考,不同水平的挑战...
WPK工具大纲!wepoker... WPK工具大纲!wepoker免费脚本咨询,德州微扑克其实是有辅助器插件1、起透看视 wepoker...
WPK透视版指引!wepoke... WPK透视版指引!wepoker怎么看底牌,德州微扑克都是是真的有透视挂工具1.wepoker怎么看...
WPK安装诀窍!wepoker... WPK安装诀窍!wepoker透视脚本苹果版,德州微扑克一直是有辅助器软件在进入wepoker透视脚...