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

相关内容

热门资讯

揭幕透视!约局吧德州透视,po... 揭幕透视!约局吧德州透视,pokemmo脚本辅助下载,经验教程(真是有挂)-哔哩哔哩pokemmo脚...
总结透视!pokermaste... 您好,pokemomo辅助软件这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054...
详情透视!we poker免费... 详情透视!we poker免费辅助器,wepoker有用吗,模板教程(有挂秘籍)-哔哩哔哩1、实时w...
开挂透视!wejoker私人辅... 开挂透视!wejoker私人辅助软件,epoker有透视吗,攻略教程(有挂透视)-哔哩哔哩1)wej...
揭幕透视!hhpoker是真的... 揭幕透视!hhpoker是真的吗,wepoker辅助软件价格,妙招教程(有挂方法)-哔哩哔哩1、全新...
揭幕透视!破解辅助插件wepo... 揭幕透视!破解辅助插件wepoker,wepoker买脚本靠谱吗,技法教程(的确有挂)-哔哩哔哩1、...
总结透视!pokemmo脚本手... 总结透视!pokemmo脚本手机版,德州透视hhpoker,大纲教程(有挂方针)-哔哩哔哩1、该软件...
必备透视!wepoker一直输... 必备透视!wepoker一直输的号能继续打吗,hhpoker是真的还是假的,妙招教程(确实有挂)-哔...
揭幕透视!hhpoker的辅助... 揭幕透视!hhpoker的辅助是真的吗,fishpoker透视底牌,绝活教程(有挂助手)-哔哩哔哩小...
揭露透视!hhpoker软件可... 揭露透视!hhpoker软件可以玩吗,aapoker万能辅助器,机巧教程(的确有挂)-哔哩哔哩一、h...