Javascript url更改,但无法导航
我有2个链接如下。当我第一次单击任何一个链接时,它会导航到该链接,但当我单击之后的第二个链接时,url会更改,但它不会导航到该链接Javascript url更改,但无法导航,javascript,angular,angular-ui-router,single-page-application,Javascript,Angular,Angular Ui Router,Single Page Application,我有2个链接如下。当我第一次单击任何一个链接时,它会导航到该链接,但当我单击之后的第二个链接时,url会更改,但它不会导航到该链接 <li><a routerLink="/order/buyer" >Buyer</a></li> <li><a routerLink="/order/seller">Seller</a></li> 订单模块.ts export const ROUTES: Routes
<li><a routerLink="/order/buyer" >Buyer</a></li>
<li><a routerLink="/order/seller">Seller</a></li>
订单模块.ts
export const ROUTES: Routes = [
{
path: ':orderParty/:id',
component: OrderDetailComponent,
canDeactivate: [OrderDetailGuardService]
},
{
path: ':orderParty',
component: OrderListComponent
}
];
尝试了几种方法,但都不起作用。我注意到,在第二次单击时,“OrderListComponent”的Ngonit()没有被调用。您有几个选项可以解决这个常见的角度问题,最常见的是在这个GitHub线程上使用解决方案: 另一个解决方案是订阅路由器参数,并根据本文建议的新参数处理更改:
this.activeRoute.params.subscribe(routeParams => {
this.loadUserDetail(routeParams.id);
});
是的,因为路线是相同的,它是正在变化的动态参数。若要读取更改的参数,可以在构造中插入路由器并读取参数,如 this.router.params.subscribe((params)=>{console.log(params)})
路由指向同一组件,因此不会重新初始化。谢谢。似乎很多人都有类似的问题。不知道为什么angular没有解决这个问题
constructor(private router: Router){
// override the route reuse strategy
this.router.routeReuseStrategy.shouldReuseRoute = function(){
return false;
}
this.router.events.subscribe((evt) => {
if (evt instanceof NavigationEnd) {
// trick the Router into believing it's last link wasn't previously loaded
this.router.navigated = false;
// if you need to scroll back to top, here is the right place
window.scrollTo(0, 0);
}
});
}
this.activeRoute.params.subscribe(routeParams => {
this.loadUserDetail(routeParams.id);
});