Angular module

  • Shared module is imported by feature modules when they need to use shared components.
  • Core module is only imported by the root application module.
  • The root application module’s router configuration associates feature modules to page routes and indicates that the modules are to be loaded lazily. More on this later.

Shared Module

Shared module contains components that are reused by multiple pages. This could be a custom Button, or a custom PasswordInputField, etc. Shared module can also export commonly imported modules such as Angular’s Common module. This way every other module that imports Shared module doesn’t need to import common modules over and over.

Declaring routing in feature modules

In a feature module you’ll have the usual component files (.ts, .html, .css) and two other files — a .module.ts file and a routing.module.ts file.

For a fictional product component (that corresponds to a product page) you’d have this in your product-routing.module.ts file。
···
// product-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ProductComponent } from './product.component';
const routes: Routes = [{ path: '', component: ProductComponent }];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class ProductRoutingModule { }
···

This is telling the routing associated with the product page to be relative to the parent routing declared in app-routing.module.ts. The module for this feature uses this feature component routing information.

···
// product.module.ts
@NgModule({
declarations: [ProductComponent],
imports: [ProductRoutingModule, SharedModule],
})
export class ProductModule {}
···
Next, we declare that this product feature module is to be loaded lazily in the root routing config.
···
// app-routing.module.ts
const routes: Routes = [{
path: '/product',
loadChildren: () => import('./feature/product/product.module')
.then((m) => m.ProductModule),
}]
···

preloading

···
@NgModule({
exports: [
RouterModule
],
imports: [
RouterModule.forRoot(appRoutes, {
preloadingStrategy: PreloadAllModules // <-This is our preloading
})
]
})

···

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 2018-3-15 星期四 一、安装 注意:请现在终端/控制台窗口中运行 node -v 和 npm -v,来验证...
    ftmo阅读 4,860评论 1 2
  • 翻译来自国外大神 上边是原文链接,英语好的可以直接进,翻译不周还望指正,共同提高. Angular 中的各种模块是...
    Ethan__Hunt阅读 4,390评论 0 2
  • 作者:王芃 wpcfan@gmail.com 第一节:初识Angular-CLI第二节:登录组件的构建第三节:建立...
    接灰的电子产品阅读 18,479评论 60 44
  • Angular介绍 Angular安装、创建项目、目录结构、组件、服务 创建组件、绑定数据、绑定属性、数据循环、条...
    地瓜粉丝阅读 3,730评论 0 2
  • What is an angular module In Angular, a module is a mecha...
    七七_77_77阅读 2,713评论 0 0

友情链接更多精彩内容