Angular 学习笔记 (Angular 9 & ivy)
refer :
https://blog.angularindepth.com/all-you-need-to-know-about-ivy-the-new-angular-engine-9cde471f42cf
https://blog.angularindepth.com/asynchronous-modules-and-components-in-angular-ivy-1c1d79d45bd3
https://blog.ninja-squad.com/2019/05/07/what-is-angular-ivy/
https://blog.angularindepth.com/angular-ivy-change-detection-execution-are-you-prepared-ab68d4231f2c (change detech)
https://blog.angularindepth.com/ivy-engine-in-angular-first-in-depth-look-at-compilation-runtime-and-change-detection-876751edd9fd (change detech)
https://netbasal.com/optimizing-angular-change-detection-triggered-by-dom-events-d2a3b2e11d87 (change detech)
听说 Angular 9 没有什么了不起的功能发布,只是将某个配置修改成默认而已. 团队真不给力丫...
修改的这个配置是渲染引擎, 名叫 ivy,好像是因为第 4 代所以叫这个名字.
它有什么不一样的地方呢 ?
主要是组件编辑后的代码不同了,对树摇友好一点. 代码好看一点. size 小一点.
1. 长相.

编辑后都把代码写进静态函数了, build 模板是直接使用调用式..elementstart 会直接调用 dom api 去创建 element (好像是这样)
注意那个 ɵfac 之后会讲到.
2. no more entry component.
以前要动态出组件挺麻烦的,要 entry component 而且 module 也不可以 lazyload
现在简单很多了
open() {
import('./abc/abc.module').then((m) => {
ɵcreateInjector(m.AbcModule, this.injector);
this.dialog.open(AbcComponent, { width: '1000px' });
});
// import('./abc/abc.component').then((m) => {
// const factory = this.cfr.resolveComponentFactory(m.AbcComponent);
// this.target.createComponent(factory, 0, this.injector);
// });
// import('./abc/abc.component').then((m) => ɵrenderComponent(m.AbcComponent,
// { injector: this.injector, host: this.el.nativeElement }
// ));
}
直接 lazy load 然后链接 injector 给这个模块,就可以用任何组件了.
模块无需 entry component 也不用 export
@NgModule({
declarations: [AbcComponent, XyzComponent],
imports: [
CommonModule
],
// exports: [AbcComponent, XyzComponent],
// entryComponents: [AbcComponent]
})
export class AbcModule { }
注意: 如果你没用 import module 而是直接 append component 你会发现也是可以跑的, 但是如果这个 component 里面有依赖其它的 component 就不行了, 除非你没用使用 module, 而是通过组件自己去写依赖.
目前官方没用给出不写 module 的方式. 相信之后会出, 可能会长的像 2.0 rc 那样,那个年代 ngmodule 还没有出来呢...
另一个重点是 injector, 我们 import 过来后, 需要用这个 module build 一个 inujector, 因为这个 module 里面可能有 provider 然后才能 append component.
3. optional ng module
ivy 后, ng module 变得是一个 optional, 当然 ng module 为我们封装了很多好料, injector, detech change, life cycle 等等.
然后你不要用它,全部都要自己弄咯. 文章里有参考,我自己没有这个需求以后有才补上 demo.
4. HOC (height order component)
目前还感受不到它能做什么大事情. 它的功能是说,现在我们可以通过 decorator 去拦截 ɵfac (这个 ɵ(Greek Theta) 是因为目前这个方法是 private 的.)
拦截后我们可以重写 ɵfac
export function HOC() {
return (cmpType) => {
const originalFactory = cmpType.ɵfac;
cmpType.ɵfac = (...args: any) => {
const cmp = originalFactory(...args);
console.log('component instance', cmp); // 重点
return cmp;
};
};
}
@HOC()
@Component({
selector: 'app-test-dialog',
templateUrl: './test-dialog.component.html',
styleUrls: ['./test-dialog.component.scss']
})
export class TestDialogComponent implements OnInit {
看重点,这里我们就可以获取到组件实例,然后可以做一个封装,比如修改一些接口等等的,可以算是装修模式吧. ngOnChange 就是用这个方式去实现的哦,它已经不是内置的 life cycle hook 了.
我们也可以通过类似方式去加强 life cycle hook.
4.I18n
ng 的 i18n 非常弱, 绝大部分功能从 v5 说要做到 v9 都没有实现.
v9 后已经被分离出来了,如果要使用的话需要 install package @angular/localize
Angular 学习笔记 (Angular 9 & ivy)的更多相关文章
- angular学习笔记-angular中怎么获取dom元素
步骤分解: 第一步:给要获取的元素一个ng-model变量,并且绑定事件啦! <div class="home" ng-model="dirName" n ...
- angular学习笔记(三十一)-$location(2)
之前已经介绍了$location服务的基本用法:angular学习笔记(三十一)-$location(1). 这篇是上一篇的进阶,介绍$location的配置,兼容各版本浏览器,等. *注意,这里介绍 ...
- angular学习笔记(三十一)-$location(1)
本篇介绍angular中的$location服务的基本用法,下一篇介绍它的复杂的用法. $location服务的主要作用是用于获取当前url以及改变当前的url,并且存入历史记录. 一. 获取url的 ...
- angular学习笔记(三十)-指令(10)-require和controller
本篇介绍指令的最后两个属性,require和controller 当一个指令需要和父元素指令进行通信的时候,它们就会用到这两个属性,什么意思还是要看栗子: html: <outer‐direct ...
- angular学习笔记(三十)-指令(7)-compile和link(2)
继续上一篇:angular学习笔记(三十)-指令(7)-compile和link(1) 上一篇讲了compile函数的基本概念,接下来详细讲解compile和link的执行顺序. 看一段三个指令嵌套的 ...
- angular学习笔记(三十)-指令(7)-compile和link(1)
这篇主要讲解指令中的compile,以及它和link的微妙的关系. link函数在之前已经讲过了,而compile函数,它和link函数是不能共存的,如果定义了compile属性又定义link属性,那 ...
- angular学习笔记(三十)-指令(6)-transclude()方法(又称linker()方法)-模拟ng-repeat指令
在angular学习笔记(三十)-指令(4)-transclude文章的末尾提到了,如果在指令中需要反复使用被嵌套的那一坨,需要使用transclude()方法. 在angular学习笔记(三十)-指 ...
- angular学习笔记(三十)-指令(5)-link
这篇主要介绍angular指令中的link属性: link:function(scope,iEle,iAttrs,ctrl,linker){ .... } link属性值为一个函数,这个函数有五个参数 ...
- angular学习笔记(三十)-指令(2)-restrice,replace,template
本篇主要讲解指令中的 restrict属性, replace属性, template属性 这三个属性 一. restrict: 字符串.定义指令在视图中的使用方式,一共有四种使用方式: 1. 元素: ...
随机推荐
- Java 泛型 (Generics)
泛型:就是变量类型的参数化 泛型是JDK1.5中的一个最重要的特征.通过引入泛型,我们将获得编译时类型的安全和运行时更小的抛出ClassCastException的可能. public class A ...
- python统计apache、nginx访问日志IP访问次数并且排序(显示前20条)【转】
前言:python统计apache.nginx访问日志IP访问次数并且排序(显示前20条).其实用awk+sort等命令可以实现,用awk数组也可以实现,这里只是用python尝试下. apach ...
- Kafka的Topic、Partition和Message
Kafka的Topic和Partition Topic Topic是Kafka数据写入操作的基本单元,可以指定副本 一个Topic包含一个或多个Partition,建Topic的时候可以手动指定Par ...
- 批量管理工具:pssh/ansible
ssh 免密码 批量管理1.创建用户useradd user1echo “123456”| passwd --stdin user12.创建秘钥ssh-keygen -t dsa然后一直回车 非交互式 ...
- https://github.com/python/cpython/blob/master/Doc/library/contextlib.rst 被同一个线程多次获取的同步基元组件
# -*- coding: utf-8 -*- import time from threading import Lock, RLock from datetime import datetime ...
- python 设计模式之原型模式 Prototype Pattern
#引入 例子1: 孙悟空拔下一嘬猴毛,轻轻一吹就会变出好多的孙悟空来. 例子2:寄个快递下面是一个邮寄快递的场景:“给我寄个快递.”顾客说.“寄往什么地方?寄给……?”你问.“和上次差不多一样,只是邮 ...
- ISO/IEC 9899:2011 条款6.2——概念
6.2 概念 6.2.1 标识符的作用域 6.2.2 标识符的连接 6.2.3 标识符的名字空间 6.2.4 对象的存储持久性 6.2.5 类型 6.2.6 类型的表示 6.2.7 兼容类型与组合类型 ...
- 软件定义网络基础---OpenFlow协议
一:OpenFlow协议概述 OpenFlow协议为控制器与交换机之间的通信,提供了一种开放标准的方式.OpenFlow交换机通过安全通道与控制器进行信息交互 二:OpenFlow消息类型 (一)co ...
- 安卓 android studio 报错 Lint found fatal errors while assembling a release target
报错截图如下: 解决方法:在app的build.gradle中添加如下代码 android{ lintOptions { checkReleaseBuilds false abortOnError f ...
- haproxy高可用、负载均衡集群
HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.根据官方数据,其最高极限支持10G的并发.另外其支持从4层至7层的网络交换, ...