angular的routerLink重定向到组件时表现正常,但后续的router.url无法返回当前URL。
创始人
2024-10-24 09:31:15
0

可以通过在组件内部订阅路由事件来获取当前URL。具体而言,可以在组件内声明路由对象,并在ngOnInit方法中订阅路由事件。这样,可以在路由事件中更新当前URL,并在需要时使用该URL。

示例代码如下:

import { Component, OnInit } from '@angular/core'; import { Router, ActivatedRoute, NavigationEnd } from '@angular/router';

@Component({ selector: 'app-example', templateUrl: './example.component.html', styleUrls: ['./example.component.css'] }) export class ExampleComponent implements OnInit {

currentUrl: string;

constructor(private router: Router, private activatedRoute: ActivatedRoute) { }

ngOnInit() { this.router.events.subscribe((event) => { if (event instanceof NavigationEnd) { this.currentUrl = event.url; } }); }

}

在上述示例中,我们使用router.events来订阅路由事件,并在NavigationEnd事件发生时更新当前URL。然后,我们可以在组件中使用currentUrl属性来访问当前URL。请注意,这只是一种解决方法,还有其他方法可以解决该问题。

相关内容

热门资讯

透视代打!aapoker辅助器... 透视代打!aapoker辅助器是真的吗(透视)丫丫陕西扑克绿色透视(本来是真的辅助软件)-哔哩哔哩1...
透视软件!wepoker破解版... 透视软件!wepoker破解版内购(透视)兴动互娱游戏辅助器好用吗(果然真的有辅助软件)-哔哩哔哩1...
据了解!wpk刷入池率脚本(透... 据了解!wpk刷入池率脚本(透视)新蜜瓜大厅免费控制器(其实存在有辅助app)-哔哩哔哩1、任何新蜜...
今日!aapoker透视怎么用... 今日!aapoker透视怎么用(透视)传送屋有挂吗(真是是真的辅助工具)-哔哩哔哩1、传送屋有挂吗免...
透视辅助!wepoker科技辅... 透视辅助!wepoker科技辅助器(透视)wepoker辅助器是真的吗(切实是真的辅助下载)-哔哩哔...
透视真的!wepoker透视辅... 透视真的!wepoker透视辅助下载(透视)天酷辅助器(真是真的有辅助神器)-哔哩哔哩1、下载好天酷...
最终!wepoker国外版透视... 最终!wepoker国外版透视(透视)hhpoker大菠萝有没有作比(本来真的是有辅助安装)-哔哩哔...
透视线上!wepoker辅助工... 透视线上!wepoker辅助工具(透视)余干中至小程序辅助器免费(果然有辅助软件)-哔哩哔哩该软件可...
透视真的!wpk官网下载链接(... 透视真的!wpk官网下载链接(透视)卡农大厅辅助(好像真的是有辅助app)-哔哩哔哩小薇(辅助器软件...
近日!WePoKer辅助器(透... 近日!WePoKer辅助器(透视)悠闲游戏辅助(总是真的有辅助神器)-哔哩哔哩1、玩家可以在悠闲游戏...