Angular嵌套模块无法加载嵌套的router-outlet。
创始人
2024-10-29 03:00:37
0

此问题通常是因为在子模块中没有正确配置路由而导致的。在子模块中,需要在路由配置中指定嵌套的router-outlet才能正确加载子模块的内容。以下是一个示例代码,展示了如何正确配置子模块的路由:

首先,在app.module.ts文件中导入子模块并将其添加到imports数组中:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ChildModule } from './child/child.module'; 
import { RouterModule } from '@angular/router';

@NgModule({
  imports: [BrowserModule, ChildModule, RouterModule.forRoot([])],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule { }

然后,在child.module.ts文件中导入子组件并定义子模块的路由:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ChildComponent } from './child.component';
import { RouterModule, Routes } from '@angular/router';
import { GrandchildComponent } from './grandchild/grandchild.component';

const routes: Routes = [
  {
    path: '',
    component: ChildComponent,
    children: [
      {
        path: 'grandchild',
        component: GrandchildComponent
      }
    ]
  }
];

@NgModule({
  imports: [CommonModule, RouterModule.forChild(routes)],
  declarations: [ChildComponent, GrandchildComponent],
  exports: [RouterModule]
})
export class ChildModule { }

在上面的示例代码中,我们首先导入了子模块和路由模块,然后在AppModule中将子模块添加到imports数组中。接着,在ChildModule中定义路由,并在RouterModule中调用forChild()函数来指定子路由。在GrandchildComponent的路由配置中,我们使用点语法来指定嵌套的路由。

在以上示例代码中,我们使用了子路由,但是我们必须在父模块中添加嵌套的router-outlet才能正确加载子模块中的子组件。在AppModule中的AppComponent模板中添加一个router-outlet元素,如下所示:


这个router-outlet元素将作为子模块的容器,用于显示子模块

相关内容

热门资讯

举措透视!wepoker游戏安... 举措透视!wepoker游戏安装教程,wepoker有辅助插件吗,第三方教程(有挂秘笈)-哔哩哔哩>...
发现透视"wepok... 发现透视"wepoker辅助工具好用吗"开挂(透视)辅助器(9分钟了解高科技教程)发现透视"wepo...
模块透视!hhpoker可以控... 【亲,hhpoker可以控制吗 这款游戏可以开挂的,确实是有挂的,很多玩家在这款hhpoker可以控...
实测透视"微扑克发牌... 实测透视"微扑克发牌机制详解"开挂(透视)辅助器(玩家亲测黑科技教程)实测透视"微扑克发牌机制详解"...
秘籍透视!wepoker可以透... >>您好:wepoker辅助器官方确实是有挂的,很多玩家在这款wepoker辅助器官方游戏中打牌都会...
正版透视"wepok... 正版透视"wepoker透视软件下载"开挂(透视)辅助软件(科技分享揭秘教程)正版透视"wepoke...
模块透视!pokemmo辅助官... wepoker模拟器哪个好用 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四...
曝光透视"微扑克辅助... 曝光透视"微扑克辅助期是干嘛的"开挂(透视)辅助工具(记者发布总结教程)曝光透视"微扑克辅助期是干嘛...
大纲透视!约局吧辅助脚本,aa... aapoker安装包怎么使用是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用...
必看透视"微扑克辅助... 必看透视"微扑克辅助器官网入口"开挂(透视)辅助脚本(今日公布必赢教程)必看透视"微扑克辅助器官网入...