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。请注意,这只是一种解决方法,还有其他方法可以解决该问题。

相关内容

热门资讯

2024教程!wpk俱乐部是真... 2024教程!wpk俱乐部是真的吗(辅助挂)真是是有挂(有挂方式)1、下载好wpk俱乐部是真的吗正确...
重磅来袭!wepoker透视脚... 重磅来袭!wepoker透视脚本(辅助挂)确实存在有挂(确实有挂)wepoker透视脚本透视方法中分...
热点讨论!德扑圈有透视吗(辅助... 热点讨论!德扑圈有透视吗(辅助挂)本来真的是有挂(揭秘有挂)亲,关键说明,德扑圈有透视吗透视脚本安卓...
每日必看教程!wepoker底... 您好,wepoker底牌透视这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】...
重大通报!we poker插件... 重大通报!we poker插件(辅助挂)确实真的是有挂(真的有挂)1、许多玩家不知道we poker...
揭秘关于!哈糖大菠萝有挂吗(辅... 揭秘关于!哈糖大菠萝有挂吗(辅助挂)都是是真的有挂(果真有挂)1、每一步都需要思考,不同水平的挑战哈...
实测交流!wepoker正确养... 实测交流!wepoker正确养号方法(辅助挂)原来有挂(有挂方法)1、wepoker正确养号方法模拟...
盘点一款!newpoker脚本... 盘点一款!newpoker脚本(辅助挂)一贯是有挂(有挂秘笈)1、newpoker脚本模拟器是什么优...
热门推荐!wepoker透视a... 热门推荐!wepoker透视app下载(辅助挂)竟然是有挂(有挂教学)该软件可以轻松地帮助玩家将we...
一分钟带你了解!wepoker... 一分钟带你了解!wepoker游戏下载(辅助挂)其实有挂(有挂方针)1)wepoker游戏下载有没有...