延迟加载功能模块不起作用
Lazy loading feature module not working
我有一个 app.routing.ts 文件如下
import { ModuleWithProviders, NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { SalesComponent } from './advisory/sales.component';
const appRoutes: Routes = [
{
path:'admin',loadChildren:'app/auth/admin/admin.module#AdminModule'
},
{
path: '',
redirectTo: '/main',
pathMatch: 'full'
},
{
path: 'main', component: SalesComponent,
data: { apptitle: 'main', subtitle: 'main' }
},
];
@NgModule({
imports: [RouterModule.forRoot(appRoutes,
{ useHash: true },
)],
exports: [RouterModule]
})
export class AppRoutingModule { }
和一个功能模块admin.module.ts文件如下
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AdminComponent } from './admin.component';
import { RouterModule } from '@angular/router';
@NgModule({
imports: [
CommonModule,
RouterModule.forChild([{
path: '',
component: AdminComponent,
}
]),
declarations: [AdminComponent]
})
export class AdminModule { }
如果我在浏览器中点击 localhost:4200/#/main,它可以正常工作,但如果我输入 localhost:4200/#/admin 或 localhost:4200/admin- 它会刷新并加载同一个主页。知道为什么会发生这种情况以及有什么建议吗?
估计路径不对,应该用相对路径,
path:'admin',loadChildren:'./auth/admin/admin.module#AdminModule'
这是一个 StackBlitz 的工作场所 https://stackblitz.com/edit/angular-f568ym?file=src%2Fapp%2Fapp.component.html
您应该使用相对路径。
path:'admin',loadChildren:'./auth/admin/admin.module#AdminModule'
此外,您可能对 AdminModule
中定义的路由有疑问
我会将 AdminModule 中的语法更改为:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AdminComponent } from './admin.component';
import { RouterModule } from '@angular/router';
const adminRoutes: Routes = [
{
path:'',
component:AdminComponent,
pathMatch: 'full'
}
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(adminRoutes)
],
declarations: [AdminComponent]
})
export class AdminModule { }
我有一个 app.routing.ts 文件如下
import { ModuleWithProviders, NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { SalesComponent } from './advisory/sales.component';
const appRoutes: Routes = [
{
path:'admin',loadChildren:'app/auth/admin/admin.module#AdminModule'
},
{
path: '',
redirectTo: '/main',
pathMatch: 'full'
},
{
path: 'main', component: SalesComponent,
data: { apptitle: 'main', subtitle: 'main' }
},
];
@NgModule({
imports: [RouterModule.forRoot(appRoutes,
{ useHash: true },
)],
exports: [RouterModule]
})
export class AppRoutingModule { }
和一个功能模块admin.module.ts文件如下
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AdminComponent } from './admin.component';
import { RouterModule } from '@angular/router';
@NgModule({
imports: [
CommonModule,
RouterModule.forChild([{
path: '',
component: AdminComponent,
}
]),
declarations: [AdminComponent]
})
export class AdminModule { }
如果我在浏览器中点击 localhost:4200/#/main,它可以正常工作,但如果我输入 localhost:4200/#/admin 或 localhost:4200/admin- 它会刷新并加载同一个主页。知道为什么会发生这种情况以及有什么建议吗?
估计路径不对,应该用相对路径,
path:'admin',loadChildren:'./auth/admin/admin.module#AdminModule'
这是一个 StackBlitz 的工作场所 https://stackblitz.com/edit/angular-f568ym?file=src%2Fapp%2Fapp.component.html
您应该使用相对路径。
path:'admin',loadChildren:'./auth/admin/admin.module#AdminModule'
此外,您可能对 AdminModule
我会将 AdminModule 中的语法更改为:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AdminComponent } from './admin.component';
import { RouterModule } from '@angular/router';
const adminRoutes: Routes = [
{
path:'',
component:AdminComponent,
pathMatch: 'full'
}
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(adminRoutes)
],
declarations: [AdminComponent]
})
export class AdminModule { }