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值。

相关内容

热门资讯

一分钟快速了解!wepoker... 此外,数据分析德州(wepoker软件安装包)辅助神器app还具备辅助透视行为开挂功能,通过对客户w...
透视ai"pokem... 1、透视ai"pokemmo手机版透视脚本"详细辅助系统教程(确实是有挂);详细教程。2、pokem...
玩家必看科普!哈糖大菠萝怎么挂... 玩家必看科普!哈糖大菠萝怎么挂,hardrock透视工具,科技教程(有挂攻略)关于哈糖大菠萝怎么挂机...
透视好友房"wepo... 透视好友房"wepokerplus作弊"详细辅助黑科技教程(素来有挂);最新版2024是一款经典耐玩...
最新通报!wepoker免费钻... 最新通报!wepoker免费钻石,约局吧开挂,切实教程(有挂技巧);亲真的是有正版授权,小编(透视辅...
透视有挂"wpk作弊... 透视有挂"wpk作弊最怕三个东西"详细辅助规律教程(素来是真的有挂)1、许多玩家不知道wpk作弊最怕...
玩家必看秘籍!wepoker透... 玩家必看秘籍!wepoker透视最简单三个步骤,wepoker辅助脚本,扑克教程(有挂技巧);实战中...
透视神器"we po... 透视神器"we poker辅助器下载"详细辅助攻略教程(素来真的有挂);大神普及一款德州ai内幕,确...
必备教程!wepoker到底有... 必备教程!wepoker到底有没有透视,aapoker公共底牌,曝光教程(有挂教程);最新版2024...
透视系统"aapok... 此外,数据分析德州(aapoker插件下载)辅助神器app还具备辅助透视行为开挂功能,通过对客户aa...