Angular 12 - 延迟加载路径模块不起作用
Angular 12 - Lazy loading path module does'nt work
我有一个作业模块,其中包含我的应用程序的各种组件。我想对所述模块进行延迟加载,但是,当我尝试通过完整路径访问时它不起作用,也就是说,如果我通过路径 http://localhost:4200/job/artist
访问它不起作用但是如果我通过http://localhost:4200/#/artist
如果有效。知道为什么会这样吗?
懒加载模块的路由模块
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { Artist } from './components/artist/artist.component';
const routes: Routes = [{
path: 'job',
loadChildren: () => import('../jobs/jobs.module').then(m => m.JobsModule)
}];
@NgModule({
imports: [
RouterModule.forRoot(routes, {
useHash: true,
}),
BrowserModule],
exports: [RouterModule]
})
export class LazyRoutingModule { }
职位-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { Artist } from './components/artist/artist.component';
const routes: Routes = [{
path: 'artist',component: ArtistComponent,
}];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class JobsRoutingModule { }
而不是这个:
const routes: Routes = [{
path: 'artist',component: ArtistComponent,
}];
试试这个:
const routes: Routes = [
{
path: '', component: ArtistComponent,
children: [
{ path: 'artist', component: ArtistComponent, },
]
}
编辑@fralejanro 改进了我的回答,并说我们必须把 'job' 而不是只是 '',为了不总是重定向到艺术家,只用 '\job\artist':
const routes: Routes = [
{
path: 'job', component: ArtistComponent,
children: [
{ path: 'artist', component: ArtistComponent, },
]
}
我有一个作业模块,其中包含我的应用程序的各种组件。我想对所述模块进行延迟加载,但是,当我尝试通过完整路径访问时它不起作用,也就是说,如果我通过路径 http://localhost:4200/job/artist
访问它不起作用但是如果我通过http://localhost:4200/#/artist
如果有效。知道为什么会这样吗?
懒加载模块的路由模块
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { Artist } from './components/artist/artist.component';
const routes: Routes = [{
path: 'job',
loadChildren: () => import('../jobs/jobs.module').then(m => m.JobsModule)
}];
@NgModule({
imports: [
RouterModule.forRoot(routes, {
useHash: true,
}),
BrowserModule],
exports: [RouterModule]
})
export class LazyRoutingModule { }
职位-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { Artist } from './components/artist/artist.component';
const routes: Routes = [{
path: 'artist',component: ArtistComponent,
}];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class JobsRoutingModule { }
而不是这个:
const routes: Routes = [{
path: 'artist',component: ArtistComponent,
}];
试试这个:
const routes: Routes = [
{
path: '', component: ArtistComponent,
children: [
{ path: 'artist', component: ArtistComponent, },
]
}
编辑@fralejanro 改进了我的回答,并说我们必须把 'job' 而不是只是 '',为了不总是重定向到艺术家,只用 '\job\artist':
const routes: Routes = [
{
path: 'job', component: ArtistComponent,
children: [
{ path: 'artist', component: ArtistComponent, },
]
}