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脚本怎么用(透视)werplan透视挂,教程烘培(有挂秘笈)-哔哩哔哩1、a...
wepoker一直输的号能继续... wepoker一直输的号能继续打吗!wepoker软件辅助程序(透视)器-本来了解是有挂暗藏猫腻,小...
透视开挂!哈糖大菠萝免费辅助器... 透视开挂!哈糖大菠萝免费辅助器(透视)hhpoker到底可以透视码,教程方案(有挂讲解)-哔哩哔哩哈...
aapoker公共底牌!wep... aapoker公共底牌!wepoker有没有透视方法(透视)教程-总是有挂是真的挂1、进入游戏-大厅...
透视曝光!wepoker透视脚... 透视曝光!wepoker透视脚本免费下载(透视)wepoker有透视底牌吗,教程技法(有挂攻略)-哔...
wepoker辅助器安装包!w... wepoker辅助器安装包!wejoker免费脚本(透视)方法-确实教你真的有挂;1、用户打开应用后...
透视了解!wpk透视辅助方法(... 透视了解!wpk透视辅助方法(透视)poker world辅助器,教程方式(有挂教程)-哔哩哔哩wp...
德州私人局脚本!wepoker... 德州私人局脚本!wepoker新号好一点吗(透视)插件-切实推荐是有挂1、wepoker新号好一点吗...
透视推荐!德普之星透视辅助软件... 透视推荐!德普之星透视辅助软件下载(透视)德普之星辅助工具如何打开,教程资料(有挂方略)-哔哩哔哩一...
hhpoker有透视的吗!wp... hhpoker有透视的吗!wpk辅助插件(透视)技巧-切实教你存在有挂运hhpoker有透视的吗辅助...