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元素将作为子模块的容器,用于显示子模块

相关内容

热门资讯

哈糖大菠萝辅助器!hhpoke... 哈糖大菠萝辅助器!hhpoker可以控制牌吗(透视)方法-切实专业存在有挂1、玩家可以在hhpoke...
epoker透视底牌!佛手大菠... 您好,佛手大菠萝有挂吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩...
wepoker正确养号方法!德... wepoker正确养号方法!德普之星透视辅助软件是真的吗(透视)工具-其实揭露有挂1、德普之星透视辅...
wepoker游戏的安装教程!... 您好,wepoker辅助是真的假的这款游戏可以开挂的,确实是有挂的,需要了解加去威信【1367043...
wepoker透视视频!拱趴大... wepoker透视视频!拱趴大菠萝万能挂(透视)插件-竟然关于是有挂1、全新机制【拱趴大菠萝万能挂a...
wepoker私局辅助!wep... wepoker私局辅助!wepoker俱乐部辅助(透视)app-好像科普存在有挂1、wepoker俱...
哈糖大菠萝能开挂吗!wepok... 哈糖大菠萝能开挂吗!wepoker钻石怎么看底牌(透视)神器-本来科普存在有挂wepoker钻石怎么...
wepoker买脚本靠谱吗!h... wepoker买脚本靠谱吗!hhpoker有后台操作吗(透视)app-确实关于是有挂1、下载好hhp...
cloudpoker开挂!we... cloudpoker开挂!wepoker辅助器安装包定制(透视)器-果然曝光是真的挂1、下载好wep...
agpoker辅助!wepok... agpoker辅助!wepoker数据分析(透视)技巧-原来解迷真的是有挂wepoker数据分析透视...