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

相关内容

热门资讯

现场直击!wpk刷入池率脚本(... 现场直击!wpk刷入池率脚本(透视)福建天天开心辅助工具视频(都是存在有辅助辅助器)-哔哩哔哩一、福...
透视系统!aapoker怎么开... 透视系统!aapoker怎么开辅助器(透视)微乐辅助软件购买平台(总是是真的辅助修改器)-哔哩哔哩1...
推出新举措!pokemmo手机... 推出新举措!pokemmo手机版脚本(透视)中至上饶打炸辅助器开挂(一直真的有辅助app)-哔哩哔哩...
透视辅助!wepoker有没有... 透视辅助!wepoker有没有辅助(透视)葫芦娃七子连心攻略(一直真的有辅助安装)-哔哩哔哩1、下载...
于此同时!hhpoker透视脚... 于此同时!hhpoker透视脚本(透视)中至小程序破解辅助器(好像有辅助安装)-哔哩哔哩中至小程序破...
透视私人局!wpk俱乐部辅助器... 透视私人局!wpk俱乐部辅助器(透视)新上游通用卦(原来存在有辅助平台)-哔哩哔哩1、任何新上游通用...
有了最新消息!pokemmo免... 有了最新消息!pokemmo免费脚本(透视)安卓手机闲逸辅助器(其实真的有辅助平台)-哔哩哔哩1、首...
透视最新!德普之星透视辅助(透... 透视最新!德普之星透视辅助(透视)微信十三张脚本(切实真的有辅助插件)-哔哩哔哩1、完成微信十三张脚...
透视透视!wepoker轻量版... 透视透视!wepoker轻量版透视(透视)花花生活圈开挂(果然是有辅助插件)-哔哩哔哩1、游戏颠覆性...
迎来新发展!德普辅助器怎么用(... 迎来新发展!德普辅助器怎么用(透视)赣牌圈科技(一直是真的辅助修改器)-哔哩哔哩1、下载好赣牌圈科技...