问题描述
我可以通过多种方式了解角度的当前路线。 我想知道以前的路线。例如在标签http:// localhost:4200 / test / testNav / ABC中输入即时消息 现在,我点击另一个选项卡 http:// localhost:4200 / test / SampleNav / XYZ
在此html“ http:// localhost:4200 / test / SampleNav / XYZ”的ngOnInit()方法中,我想获取先前的URL / ABC。
this.router.url,this.activatedRoute.snapshot都给了我当前的URL。有没有一种方法可以从可用的角度组件中获取以前的网址。
基本上,当我从ABC进入XYZ页面时,我应该做点什么,而当我从CBA进入XYZ页面时,我应该做点其他事情。任何输入都值得赞赏。
解决方法
此处getCurrentNavigation()
与可选链接(问号)一起使用,因为它可以是null
。您可以在那里previousNavigation
,finalUrl
会退还给您UrlTree
。接下来,如果它不为空,只需在finalUrl
上执行导航。
const urlTree = this.router.getCurrentNavigation()?.previousNavigation?.finalUrl;
if (urlTree) {
this.router.navigateByUrl(urlTree);
}