我已经在我的应用程序中实现了延迟加载,此后,根路由不起作用。我已经阅读了一些帖子,并且显然在延迟加载的Angular 7中不起作用,我应该从“”重定向到“任何”。关键是它不执行重定向,因此从未渲染我的HomeComponent。我尝试过从其他路由重定向到根路由,它可以进行重定向,但仍然无法呈现任何内容。我尝试过的另一件事是在另一个路径(例如“ home”)中渲染HomeComponent,但是我需要在根路由中进行一些操作,无论是重定向还是渲染。这是我的代码,预先感谢!
PS:其余路线均正常运行,仅此一条我无法使其正常运行。如果我尝试从我的应用程序渲染任何其他模块,它将执行相同的操作(不执行任何操作),并且这些模块在其他路由中正常工作。
app-routing.module.ts
...
const routes: Routes = [
// Home
{ path: '', redirectTo: 'home', pathMatch: 'full' }, // Doesn't redirect
{ path: 'home', loadChildren: './views/home/home.module#HomeModule' },
... // But it renders when you type the route in the browser
]
home.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HomeComponent } from './home.component';
import { HomeRoutingModule } from './home-routing.module';
@NgModule({
declarations: [HomeComponent],
imports: [
CommonModule,
HomeRoutingModule
]
})
export class HomeModule { }
home-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home.component';
const routes: Routes = [
{path: '', component: HomeComponent }
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class HomeRoutingModule { }
从HomeRoutingModule中删除exports: [RouterModule]
。您不需要在forChild路由模块中导出路由器模块。仅当您的子模块具有forRoot
路由时。并且您需要将该模块导入到appModule(主模块)中。