Angular 9 通用和延迟加载路由 [英] Angular 9 Universal and lazy loaded routes

查看:31
本文介绍了Angular 9 通用和延迟加载路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速提问;我找不到有关该问题的任何文档.在 Angular Universal 的早期版本中,要在 查看源代码 中呈现延迟加载的路由,您必须在 app.server.module.ts 中包含 ModuleMapLoaderModule.

Quick question; I can't find any documentation on the issue. In previous versions on Angular Universal, to render lazy loaded routes in view source you had to include ModuleMapLoaderModule in the app.server.module.ts.

在 Angular 9 中,如果您没有为 loadChildren 使用字符串,则似乎您不必这样做.我有一个新项目,我设置了这样的延迟加载:

In Angular 9, it appears you don't have to do that if you are not using strings for loadChildren. I have a new project and I have set up lazy loading like this:

const routes: Routes = [
  {
    path: '',
    loadChildren: () =>
      import('./brands/brands.module').then((m) => m.BrandsModule),
  },
  { path: '**', redirectTo: '' },
];

如果我查看页面源代码,我看不到我的 BrandsComponent 但如果我将路由更改为:

If I view the page source, I cannot see my BrandsComponent but if I change the routing to this:

const routes: Routes = [
  {
    path: '',
    component: BrandsComponent,
  },
  { path: '**', redirectTo: '' },
];

查看页面源代码会显示 BrandsComponent,它告诉我延迟加载不起作用.有谁知道如何解决这个问题?

Viewing the page source shows the BrandsComponent which tells me lazy loading is not working. Does anyone know how to fix this?

推荐答案

文档中的延迟加载基础有点误导.

您应该阅读分步设置 仔细.

我认为结构应该如下所示:

I think the structure should be like the following:

app-routing.module.ts

const routes: Routes = [
  {
    path: '',
    loadChildren: () =>
      import('./brands/brands.module').then((m) => m.BrandsModule),
  },
  { path: '**', redirectTo: '' },
];

brands-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { BrandsComponent } from './brands.component';

const routes: Routes = [
  {
    path: '',
    component: BrandsComponent
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class BrandsRoutingModule { }

brands.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { BrandsRoutingModule } from './brands-routing.module';
import { BrandsComponent } from './brands.component';

@NgModule({
  imports: [
    CommonModule,
    BrandsRoutingModule
  ],
  declarations: [BrandsComponent]
})
export class BrandsModule { }

这篇关于Angular 9 通用和延迟加载路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆