这个问题的根本原因是在重定向后,该应用程序的路由器状态没有被清除。因此,在重新加载应用程序时,路由器尝试导航到先前未被清除的状态,从而导致特定路由未被调用。
为了解决这个问题,可以通过在应用程序加载时手动清除浏览器状态来避免路由冲突。具体来说,您可以使用以下示例代码:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
constructor(private router: Router) { }
ngOnInit() {
// clear router state on app load
this.router.navigateByUrl('/refresh', { skipLocationChange: true }).then(() => {
this.router.navigate(['']);
});
}
}
在上面的代码中,我们通过使用“router.navigate”方法手动重定向到空路由来清除路由器状态,并确保应用程序加载后,路由器处于干净的状态。这应该解决在从外部URL重定向后无法重定向到特定路由的问题。