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' ...
随机推荐
- ZFS文件系统及Freenas介绍
一.简介 1.什么是zfs文件系统 ZFS文件系统的英文名称为Zettabyte File System,也叫动态文件系统(Dynamic File System),是第一个128位文件系统.最初是由 ...
- Python2与Python3兼容
Python2与Python3兼容 python3写的代码如何也能在pyhon2上跑?请无论如何加上这一句,python3没有啥影响 from __future__ import absolute_i ...
- VS使用日常
一.快捷键 1.Ctrl R+E 选中变量快捷自动生成属性
- JavaScript的书写格式及书写的注意点
JavaScript书写格式: 1.行内样式: 写在标签内部 2.内嵌样式(内联样式) : 写在一对head标签中 3.外链样式: 写在一个单独的.js文件中, 再导入进来 JavaScript书写格 ...
- git 命令提交项目到git服务器
1.先下载git,然后安装git https://git-scm.com/downloads 2.在电脑任意盘创建一个目录 3.在创建的目录下点击右键 4.初始化git 使用git init 初始化, ...
- Oracle 限制行的子句
12c新特性 限制行的子句允许限制查询返回的行.可以指定偏移量,以及要返回的行数或百分比.您可以使用此子句实现Top-N报告.要获得一致的结果,请指定ORDER_by子句以确保具有确定性的排序顺序. ...
- 使用Kerberos进行Hadoop认证
使用Kerberos进行Hadoop认证 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Kerberos是一种网络身份验证协议.它旨在通过使用秘密密钥加密为客户端/服务器应用程序提 ...
- js对属性的操作
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- PHP openssl DES加解密
不说废话上代码 加密 $str_padded = 'android'; //要加密的字符串 $iv = "12345678"; 偏移值 cbc必填 "\x01\x02\x ...
- Java精通并发-从Thread与Runnable说起
java并发的学习从去年就已经中断了,之前只对于java并发的一些基础进行了一些巩固,对于这个硬技能不管是对于面试还是对于日常的实际开发来说都非常之重要,所以接下来给自己重新定一个新目标,准备重拾它一 ...