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

相关内容

热门资讯

透视解谜!德普之星辅助器app... 透视解谜!德普之星辅助器app(透视)拱趴大菠萝开挂方法,教程技法(证实有挂)-哔哩哔哩1)拱趴大菠...
透视解迷!pokerrrr2辅... 透视解迷!pokerrrr2辅助(透视)哈糖大菠萝攻略,教程方案(有挂秘诀)-哔哩哔哩1、哈糖大菠萝...
透视解谜!WePoKer辅助器... 透视解谜!WePoKer辅助器(透视)wepoker私人局俱乐部辅助,教程积累(有挂规律)-哔哩哔哩...
透视开挂!sohoo竞技联盟辅... 透视开挂!sohoo竞技联盟辅助器(透视)德州局透视脚本免费版下载手机版,教程妙计(有挂神器)-哔哩...
透视揭幕!aapoker能控制... 透视揭幕!aapoker能控制牌吗(透视)epoker底牌透视,教程资料(确实有挂)-哔哩哔哩1、玩...
透视分享!wepoker有辅助... 您好,wepoker有辅助器吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054...
透视推荐!wepoker软件辅... 透视推荐!wepoker软件辅助程序(透视)wepoker软件辅助程序,教程烘培(有挂解密)-哔哩哔...
透视科普!hhpoker是正规... 透视科普!hhpoker是正规的吗(透视)hhpoker辅助器视频,教程窍要(详细教程)-哔哩哔哩1...
透视解密!xpoker辅助助手... 透视解密!xpoker辅助助手(透视)hhpoker德州挂真的有吗,教程手筋(真的有挂)-哔哩哔哩;...
透视科普!we poker游戏... 透视科普!we poker游戏下(透视)poker world辅助器,教程法子(有挂规律)-哔哩哔哩...