Angular无法导航到嵌套的非子组件
创始人
2024-10-30 01:00:15
0次

要导航到非子组件,我们需要使用路由对象和路由路径来手动导航。以下是一个具体的示例:

  1. 在根路径下的路由配置文件中,为非子组件添加路由路径。
const appRoutes: Routes = [
  { path: 'home', component: HomeComponent},
  { path: 'about', component: AboutComponent},
  { path: 'parent', component: ParentComponent,
    children: [
      { path: 'childOne', component: ChildOneComponent },
      { path: 'childTwo', component: ChildTwoComponent },
    ]
  },
  { path: 'non-child', component: NonChildComponent } //添加非子组件的路由路径
];
  1. 在导航组件中导入路由服务和路由对象,使用router.navigate()方法来手动导航到非子组件。
import { Component } from '@angular/core';
import { Router } from '@angular/router';

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

Parent Component

`, }) export class ParentComponent { constructor(private router: Router) {} navigateToChildOne() { this.router.navigate(['/parent/childOne']); //导航到子组件 } navigateToChildTwo() { this.router.navigate(['/parent/childTwo']); //导航到子组件 } navigateToNonChild() { this.router.navigate(['/non-child']); //导航到非子组件 } }

相关内容

热门资讯

必备辅助推荐!微乐自建房辅助工... 必备辅助推荐!微乐自建房辅助工具官网(作弊器),微乐家乡麻将一贯存在有透视挂教程微乐自建房辅助工具官...
玩家必备教程!微乐自建房辅助可... 玩家必备教程!微乐自建房辅助可信吗(透视挂),微乐河池麻将果然存在有透视挂工具1、首先打开微乐自建房...
热门推荐!微乐小程序插件免费(... 热门推荐!微乐小程序插件免费(透视器),微乐安徽麻将一直是有透视挂app在进入微乐小程序插件免费软件...
实测揭晓!微信小程序微乐辅助器... 实测揭晓!微信小程序微乐辅助器破解版(开挂),微乐提坑竟然有透视挂脚本1、超多福利:超高返利,海量正...
必备辅助推荐!微乐家乡破解版(... 必备辅助推荐!微乐家乡破解版(开挂),微乐长春麻将本来真的是有透视挂软件1、这是跨平台的微乐家乡破解...
程序员教你!微乐广西麻辣(作弊... 程序员教你!微乐广西麻辣(作弊器),微乐长沙跑胡子都是真的是有透视挂攻略微乐广西麻辣透视方法中分为三...
一分钟快速了解!小程序微乐斗地... 一分钟快速了解!小程序微乐斗地主辅助(透视挂),微乐保皇一直真的是有透视挂方法1)小程序微乐斗地主辅...
重大消息!微乐家乡游戏攻略(透... 重大消息!微乐家乡游戏攻略(透视挂),陕西微乐三代切实是有透视挂软件1、微乐家乡游戏攻略脚本辅助下载...
实测必看!陕西微乐小程序脚本(... 实测必看!陕西微乐小程序脚本(辅助),微乐哈尔滨麻将一贯存在有透视挂脚本在进入陕西微乐小程序脚本软件...
攻略讲解!微乐江苏小程序游戏破... 攻略讲解!微乐江苏小程序游戏破解下载(开挂),微乐小程序麻将切实有透视挂攻略;1、很好的工具软件,可...