4

我有一个作业模块,其中包含我的应用程序的各种组件。我想对所述模块进行延迟加载,但是,当我尝试通过完整路径访问时它不起作用,也就是说,如果我通过路径访问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 { }

作业路由.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 { }
4

1 回答 1

3

而不是这个:

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, },         
    ]
  }
于 2021-07-19T17:47:50.530 回答