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

相关内容

热门资讯

事发当天!wepoker透视脚... 事发当天!wepoker透视脚本视频(透视)果然有辅助安装(有挂助手)-哔哩哔哩1、该软件可以轻松地...
透视实锤!wpk模拟器是什么(... 透视实锤!wpk模拟器是什么(透视)都是存在有辅助软件(真是有挂)-哔哩哔哩一、wpk模拟器是什么可...
长期以来!约局吧开挂(透视)其... 长期以来!约局吧开挂(透视)其实是有辅助下载(有挂教程)-哔哩哔哩约局吧开挂辅助器是一种具有地方特色...
透视玄学!拱趴大菠萝开挂方法(... 透视玄学!拱趴大菠萝开挂方法(透视)总是是有辅助安装(果真有挂)-哔哩哔哩1、用户打开应用后不用登录...
记者获悉!newpoker脚本... 记者获悉!newpoker脚本(透视)竟然真的是有辅助下载(发现有挂)-哔哩哔哩1、实时newpok...
透视苹果版!wepoker透视... 透视苹果版!wepoker透视脚本下载(透视)原来有辅助插件(讲解有挂)-哔哩哔哩1)wepoker...
透视辅助!hhpoker视频巡... 透视辅助!hhpoker视频巡查真的假的(透视)都是是有辅助软件(有挂工具)-哔哩哔哩1、透视辅助!...
随着!pokermaster脚... 随着!pokermaster脚本(透视)一直有辅助软件(有挂实锤)-哔哩哔哩1、进入游戏-大厅左侧-...
透视了解!pokemmo修改器... 透视了解!pokemmo修改器手机版(透视)竟然是有辅助app(真的有挂)-哔哩哔哩1、首先打开po...
这一问题亟待解决!wepoke... 您好,wepoker辅助器安装包这款游戏可以开挂的,确实是有挂的,需要了解加去威信【48527505...