Angular路由器.navigate只在子路由上起作用一次
创始人
2024-10-28 03:00:02
0

要解决Angular路由器.navigate只在子路由上起作用一次的问题,可以使用路由器的导航结束事件(NavigationEnd)来重新导航到当前子路由。

以下是一个示例代码:

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

@Component({
  selector: 'app-parent-component',
  template: `
    

Parent Component

`, }) export class ParentComponent implements OnInit { constructor(private router: Router) { } ngOnInit() { this.router.events.subscribe((event) => { if (event instanceof NavigationEnd) { const childRoute = this.router.routerState.snapshot.root.firstChild; if (childRoute) { this.router.navigate([childRoute.routeConfig.path]); } } }); } navigateToChild() { this.router.navigate(['child']); } }

在上面的示例代码中,我们定义了一个ParentComponent作为父组件,并在模板中放置了一个按钮用于导航到子路由。在ngOnInit方法中,我们订阅了路由器的导航结束事件,并在事件处理程序中获取当前子路由并重新导航到该子路由。

这样,无论是通过点击按钮导航到子路由,还是通过其他方式导航到子路由,都会确保每次导航到子路由时都会重新导航一次,从而解决了只在子路由上起作用一次的问题。

相关内容

热门资讯

德州之星辅助挂!aapoker... 德州之星辅助挂!aapoker俱乐部后台可以操作吗(透视)好像真的是有挂(详细辅助规律教程)1、不需...
透视游戏!aapoker透明挂... 透视游戏!aapoker透明挂,wepoke支持安卓吗,详细透视扑克教程1、全新机制【aapoker...
wepoke是真的有挂!云扑克... wepoke是真的有挂!云扑克辅助(透视)从前真的是有挂(详细辅助2025版教程)是一款可以让一直输...
透视有挂!wepoke辅助有挂... 透视有挂!wepoke辅助有挂,德州poker有外挂吗,详细透视透明挂教程1、wepoke辅助有挂系...
德扑之星有作弊!德州ai机器人... 德扑之星有作弊!德州ai机器人辅助(透视)一贯是有挂(详细辅助科技教程);德扑之星有作弊!德州ai机...
透视好友!wepoke有没有挂... 透视好友!wepoke有没有挂,wepoke 软件,详细透视解密教程wepoke有没有挂辅助器中分为...
德州AI智能辅助机器人!微扑克... 德州AI智能辅助机器人!微扑克辅助机器人(透视)原先存在有挂(详细辅助2025新版教程)您好,德州A...
透视挂透视!wepokeai机... 透视挂透视!wepokeai机器人,wepoke有挂吗,详细透视AI教程1、wepokeai机器人透...
智星德州菠萝偷偷看牌功能!德扑... 智星德州菠萝偷偷看牌功能!德扑ai自定义设置数据(透视)起初是真的有挂(详细辅助实用技巧)1、许多玩...
aapoker有猫腻!wepo... aapoker有猫腻!wepoke小技巧(透视)一向是真的有挂(详细辅助黑科技教程)1、aapoke...