AngularRouter的URL没有更新当前路径
创始人
2024-10-29 06:00:47
0

在Angular中,当使用Router导航时,URL可能不会实时更新,导致当前路径不正确。为了解决这个问题,可以尝试以下两种方法:

方法1:使用Angular的ngZone来强制 Zone 进行变化检测。

import { Component, NgZone } from '@angular/core'; import { Router } from '@angular/router';

@Component({ selector: 'app-my-component', template: ``, }) export class MyComponent { constructor(private router: Router, private ngZone: NgZone) {}

navigateToRoute(route: string): void { this.ngZone.run(() => { this.router.navigate([route]); }); } }

方法2:在Router事件监听器中使用Angular的ChangeDetectorRef来强制更新视图。

import { ChangeDetectorRef, Component } from '@angular/core'; import { Router, NavigationEnd } from '@angular/router';

@Component({ selector: 'app-main', template: {{ currentUrl }}, }) export class MainComponent { currentUrl: string;

constructor(private router: Router, private cdr: ChangeDetectorRef) {}

ngOnInit() { this.router.events.subscribe(event => { if (event instanceof NavigationEnd) { this.currentUrl = event.url; this.cdr.detectChanges(); } }); } }

这两种方法都可以强制更新视图,保证URL和当前路径保持一致。

相关内容

热门资讯

为了进一步!hhpoker怎么... 为了进一步!hhpoker怎么破解(透视)原来是有挂(有挂方针开挂辅助下载)-哔哩哔哩1、完成hhp...
近日!智星菠萝透视,智星菠萝透... 近日!智星菠萝透视,智星菠萝透视,模板教程(有挂透明挂)-哔哩哔哩1、全新机制【智星菠萝透视软件透明...
截至目前!wepoker免费脚... 截至目前!wepoker免费脚本咨询(透视)原来有挂(有挂秘笈开挂辅助平台)-哔哩哔哩1、任何wep...
有了最新消息!xpoker辅助... 有了最新消息!xpoker辅助神器,wepoker怎么发冤家牌,策略教程(有挂教程)-哔哩哔哩1、在...
据通报!wpk模拟器(透视)其... 据通报!wpk模拟器(透视)其实是有挂(有挂分享开挂辅助插件)-哔哩哔哩wpk模拟器辅助器中分为三种...
长期以来!拱趴大菠萝万能挂图解... 长期以来!拱趴大菠萝万能挂图解,steampokermaster辅助,方针教程(有挂规律)-哔哩哔哩...
昨日!德普之星透视免费(透视)... 昨日!德普之星透视免费(透视)果然是有挂的(有挂详情开挂辅助辅助器)-哔哩哔哩一、德普之星透视免费A...
备受关注的!哈糖大菠萝助手,p... 备受关注的!哈糖大菠萝助手,pokemmo辅助工具,课程教程(有挂详细)-哔哩哔哩1、任何哈糖大菠萝...
围绕透视问题!aapoker透... 围绕透视问题!aapoker透视脚本入口(透视)一贯确实有挂(有挂方式开挂辅助下载)-哔哩哔哩所有人...
备受关注的!wepoker俱乐... 备受关注的!wepoker俱乐部辅助,wepoker黑侠辅助器,指南书教程(真的有挂)-哔哩哔哩1、...