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

相关内容

热门资讯

透视专业!wepokerplu... 透视专业!wepokerplus辅助(透视)wpk透视是真的吗,教程教材(有挂攻略)-哔哩哔哩1、每...
WePOker有没有透视方法!... WePOker有没有透视方法!aapoker怎么设置提高好牌几率(透视)技巧-总是了解真的是有挂1、...
透视揭露!wepokerplu... 透视揭露!wepokerplus开挂(透视)wpk透视最怕三个东西,教程法子(有挂头条)-哔哩哔哩1...
hhpkoer辅助挂是真的吗!... hhpkoer辅助挂是真的吗!wepoker辅助透视软件(透视)方法-其实专业存在有挂在进入hhpk...
透视关于!hhpkoer辅助器... 透视关于!hhpkoer辅助器视频(透视)hhpoker为什么一直输,教程教材(有挂头条)-哔哩哔哩...
aapoker插件!cloud... aapoker插件!cloudpoker怎么开挂(透视)软件-真是科普是真的挂1、这是跨平台的clo...
透视科普!来玩德州破解器(透视... 透视科普!来玩德州破解器(透视)wepoker透视有用吗,教程学习(有挂助手)-哔哩哔哩1、不需要A...
hhpoker德州透视!pok... hhpoker德州透视!pokemmo手机脚本(透视)app-本来开挂真的是有挂1、完成pokemm...
透视教你!werplan透视挂... 您好,pokemmo辅助官网这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】...
佛手在线大菠萝技巧!werpl... 佛手在线大菠萝技巧!werplan开挂(透视)攻略-一直透视有挂1、佛手在线大菠萝技巧有没有辅助教程...