Angular惰性加载的特性模块
一:Angular-CLI建立应用
cmd命令:ng new lazy-app --routing (创建一个名叫 lazy-app 的应用,而 --routing 标识生成了一个名叫 app-routing.module.ts 的文件)
cd lazy-app (进入lazy-app项目)
二:创建一个带路由的特性模块
cmd命令:ng generate module customers --routing (创建一个 customers 目录,其中有两个文件:CustomersModule 和 CustomersRoutingModule)
备注:如果出现 Error: Cannot find module '@angular-devkit/core',那就 npm install @angular-devkit/core
:向特性模块添加组件
cmd命令:ng generate component customers/customer-list (在 customers 目录中创建一个名叫 customer-list 的文件夹,其中包含该组件的四个文件)
:再添加一个特性模块
cmd命令:ng generate module orders --routing
ng generate component orders/order-list
三:建立UI
app.component.html
<h1>
{{title}}
</h1> <button routerLink="/customers">Customers</button>
<button routerLink="/orders">Orders</button>
<button routerLink="">Home</button> <router-outlet></router-outlet>
四:配置路由

4.1:顶级路由:app-routing.module.ts (惰性加载的语法:loadChildren 后面紧跟着一个字符串,它指向模块路径,然后是一个 #,然后是该模块的类名)
const routes: Routes = [
{
path: 'customers',
loadChildren: 'app/customers/customers.module#CustomersModule'
},
{
path: 'orders',
loadChildren: 'app/orders/orders.module#OrdersModule'
},
{
path: '',
redirectTo: '',
pathMatch: 'full'
}
];
4.2:配置特性模块的路由
customers-routing.module.ts:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { CustomerListComponent } from './customer-list/customer-list.component';
const routes: Routes = [
{
path: '',
component: CustomerListComponent
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class CustomersRoutingModule { }
orders-routing.module.ts:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { OrderListComponent } from './order-list/order-list.component';
const routes: Routes = [
{
path: '',
component: OrderListComponent
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class OrdersRoutingModule { }
五:正常工作
cmd命令:ng serve --open
初始化后台:

点击【Customers】后台

点击【Orders】后台

备注:forRoot() 包含的注入器配置是全局性的,比如对路由器的配置。forChild() 中没有注入器配置,只有像 RouterOutlet 和 RouterLink 这样的指令。
Angular惰性加载的特性模块的更多相关文章
- angular惰性加载拓展剖析
最近把一个比较旧的业余项目重新升级了下,将主文件进行了剥离,增加了些惰性加载的配置,将过程中一些零散的知识点做个总结,同时尽量深入原理实现层面. 项目环境: 前端框架:angular2.0.0-bet ...
- Angular - 预加载 Angular 模块
Angular - 预加载延迟模块 在使用路由延迟加载中,我们介绍了如何使用模块来拆分应用,在访问到这个模块的时候, Angular 加载这个模块.但这需要一点时间.在用户第一次点击的时候,会有一点延 ...
- Angular 懒加载找不到模块问题解决方法
问题: 懒加载无法找到模块 解决办法: 在app-routing.module.ts中引入该模块
- 关于angular5的惰性加载报错问题
之前为了测试一个模块优化问题,于是用angular-cli快速搭建了个ng5的脚手架demo,在应用惰性加载功能的时候发现浏览器报错如下: ERROR Error: Uncaught (in prom ...
- Angular2 ng2 如何配置惰性加载
需要修改至少四个地方1. 将子组件进行模块化操作2.生成子组件module .子组件router3.配置主路由 信息 改为loadChild4.配置appModule 删除引入 以product组件 ...
- angular懒加载的一些坑
写在前面 最近在工作中接触到angular模块化打包加载的一些内容,感觉中间踩了一些坑,在此标记一下. 项目背景: 项目主要用到angularJs作为前端框架,项目之前发布的时候会把所有的前端脚本打包 ...
- 雷林鹏分享:jQuery EasyUI 树形菜单 - 树形网格惰性加载节点
jQuery EasyUI 树形菜单 - 树形网格惰性加载节点 有时我们已经得到充分的分层树形网格(TreeGrid)的数据. 我们还想让树形网格(TreeGrid)按层次惰性加载节点. 首先,只加载 ...
- Windows加载器与模块初始化
本文是Matt Pietrek在1999年9月的MSJ杂志上发表的关于Windows加载器与模块初始化方面的文章.作者深入分析了LdrpRunInitialize璕outines例程的作用,用C语言写 ...
- 根据配置文件加载js依赖模块(JavaScript面试题)
面试题目 根据下面的配置文件 module=[ {'name':'jquery','src':'/js/lib/jquery-1.8.3.js'}, {'name':'swfobject','src' ...
随机推荐
- axios ios 微信浏览器session问题
在ios系统下,微信浏览器使用axios 可能存在seesion不存在的问题,其原因是因为存在跨域 解决方案如下 1.修改域名为同一域名 2.后台允许跨域
- 修改CentOS默认yum源为国内镜像
参考文档 https://blog.csdn.net/inslow/article/details/54177191 国内主要开源的开源镜像站点应该是网易和阿里云了. 修改为163yum源-mirro ...
- Attribute与Property关系
总的来说,其实是HTML Attribute 与 DOM property之间的关系. 1 什么是Property? JS DOM Object对象有property.一个property可能是不同数 ...
- vue应用难点总结
一.父子组件生命周期 父组件create->子组件create->子组件mounted->父组件mounted 当一个钩子函数使用了异步请求时,并不会等该钩子函数中所有异步的回调都执 ...
- SVN上文件出现左侧黄色箭头右侧绿色箭头的双向箭头
转自:https://blog.csdn.net/jiuweihu521/article/details/90902152 与资源库对比又没有要提交的东西,网上说删除这个目录,然后更新整个配置库..我 ...
- Map转url ,url转Map工具类
/** * 将url参数转换成map * @param param aa=11&bb=22&cc=33 * @return */ public static Map<String ...
- python3 提示sqlite模块不存在
首先yum install sqlite-devel -y 然后重装下python3(一定要重装)# cd Python-3.4.2# ./configure --prefix=/usr/local/ ...
- iview 标题内边距过大; 调整iview 单元格内边距、行高;
1css代码: /*调整table cell间隔和行高*/ .ivu-table-cell { padding-left: 1px; padding-right: 1px; } .ivu-table- ...
- p1.BTC-密码学的原理
所谓加密货币是不加密的,区块链上所有的交易内容(包括:账户的地址,转账的地址)都是公开的. Bitcoin中主要用到密码学的中的两个功能:Hash和签名. 一 Hash Cryptographic h ...
- 数据库PDO简介
php简介,php历史,php后端工程师职业前景,php技术方向,php后端工程师职业体系介绍. php是世界上使用最广泛的web开发语言,是超文本预处理器,是一种通用的开源脚本语言,语法吸收了c语言 ...