情之润 發表於 2019-11-5 21:04:00

Angular 学习笔记 (Angular 9 & ivy)

<p>refer :&nbsp;</p>
<p>https://blog.angularindepth.com/all-you-need-to-know-about-ivy-the-new-angular-engine-9cde471f42cf</p>
<p>https://blog.angularindepth.com/asynchronous-modules-and-components-in-angular-ivy-1c1d79d45bd3</p>
<p>https://blog.angularindepth.com/the-future-of-standalone-components-in-the-post-ivy-release-days-e7ed9b9b4dcd</p>
<p>https://blog.nrwl.io/metaprogramming-higher-order-components-and-mixins-with-angular-ivy-75748fcbc310</p>
<p>https://www.softwarearchitekt.at/aktuelles/architecture-with-ivy-a-possible-future-without-angular-modules/</p>
<p>https://www.softwarearchitekt.at/aktuelles/architecture-with-angular-ivy-part-2-higher-order-and-dynamic-components/</p>
<p>https://blog.ninja-squad.com/2019/05/07/what-is-angular-ivy/</p>
<p>https://blog.angularindepth.com/angular-ivy-change-detection-execution-are-you-prepared-ab68d4231f2c&nbsp;(change detech)</p>
<p>https://blog.angularindepth.com/ivy-engine-in-angular-first-in-depth-look-at-compilation-runtime-and-change-detection-876751edd9fd&nbsp;&nbsp;(change detech)</p>
<p>https://netbasal.com/optimizing-angular-change-detection-triggered-by-dom-events-d2a3b2e11d87&nbsp;(change detech)</p>
<p>&nbsp;</p>
<p>听说 Angular 9 没有什么了不起的功能发布,只是将某个配置修改成默认而已. 团队真不给力丫...</p>
<p>修改的这个配置是渲染引擎, 名叫 ivy,好像是因为第 4 代所以叫这个名字.&nbsp;</p>
<p>它有什么不一样的地方呢 ?&nbsp;</p>
<p>主要是组件编辑后的代码不同了,对树摇友好一点. 代码好看一点. size 小一点.</p>
<p>&nbsp;</p>
<p>1. 长相.</p>
<p><img src="https://img2018.cnblogs.com/blog/641294/201911/641294-20191105204908662-361965900.png"></p>
<p>&nbsp;编辑后都把代码写进静态函数了, build 模板是直接使用调用式..elementstart 会直接调用 dom api 去创建 element (好像是这样)</p>
<p>注意那个 ɵfac 之后会讲到.</p>
<p>&nbsp;</p>
<p>2. no more entry component.&nbsp;</p>
<p>以前要动态出组件挺麻烦的,要 entry component 而且 module 也不可以 lazyload</p>
<p>现在简单很多了</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">open() {
    import(</span>'./abc/abc.module').then((m) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      ɵcreateInjector(m.AbcModule, </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.injector);
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.dialog.open(AbcComponent, { width: '1000px'<span style="color: rgba(0, 0, 0, 1)"> });
    });

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> import('./abc/abc.component').then((m) =&gt; {</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   const factory = this.cfr.resolveComponentFactory(m.AbcComponent);</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   this.target.createComponent(factory, 0, this.injector);</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> });</span>

    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> import('./abc/abc.component').then((m) =&gt; ɵrenderComponent(m.AbcComponent,</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   { injector: this.injector, host: this.el.nativeElement }</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ));</span>
}</pre>
</div>
<p>直接 lazy load 然后链接 injector 给这个模块,就可以用任何组件了.</p>
<p>模块无需 entry component 也不用 export</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">@NgModule({
declarations: ,
imports: [
    CommonModule
],
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> exports: ,</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> entryComponents: </span>
<span style="color: rgba(0, 0, 0, 1)">})
export class AbcModule { }</span></pre>
</div>
<p>注意:&nbsp; 如果你没用 import module 而是直接 append component 你会发现也是可以跑的, 但是如果这个 component 里面有依赖其它的 component 就不行了, 除非你没用使用 module, 而是通过组件自己去写依赖.&nbsp;</p>
<p>目前官方没用给出不写 module 的方式. 相信之后会出, 可能会长的像 2.0 rc 那样,那个年代 ngmodule 还没有出来呢...&nbsp;</p>
<p>另一个重点是 injector, 我们 import 过来后, 需要用这个 module build 一个 inujector, 因为这个 module 里面可能有 provider 然后才能 append component.&nbsp;</p>
<p>&nbsp;&nbsp;</p>
<p>&nbsp;</p>
<p>3. optional ng module&nbsp;</p>
<p>ivy 后, ng module 变得是一个 optional, 当然 ng module 为我们封装了很多好料, injector, detech change, life cycle 等等.&nbsp;</p>
<p>然后你不要用它,全部都要自己弄咯. 文章里有参考,我自己没有这个需求以后有才补上 demo.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>4. HOC (height order component)</p>
<p>目前还感受不到它能做什么大事情. 它的功能是说,现在我们可以通过 decorator 去拦截 ɵfac (这个 ɵ(Greek Theta) 是因为目前这个方法是 private 的.)</p>
<p>拦截后我们可以重写 ɵfac&nbsp;</p>
<div class="cnblogs_code">
<pre>export <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> HOC() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> (cmpType) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    const originalFactory </span>=<span style="color: rgba(0, 0, 0, 1)"> cmpType.ɵfac;
    cmpType.ɵfac </span>= (...args: any) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      const cmp </span>=<span style="color: rgba(0, 0, 0, 1)"> originalFactory(...args);
      console.log(</span>'component instance'<span style="color: rgba(0, 0, 0, 1)">, cmp); // 重点
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> cmp;
    };
};
}

@HOC()
@Component({
selector: </span>'app-test-dialog'<span style="color: rgba(0, 0, 0, 1)">,
templateUrl: </span>'./test-dialog.component.html'<span style="color: rgba(0, 0, 0, 1)">,
styleUrls: [</span>'./test-dialog.component.scss'<span style="color: rgba(0, 0, 0, 1)">]
})
export class TestDialogComponent implements OnInit {</span></pre>
</div>
<p>看重点,这里我们就可以获取到组件实例,然后可以做一个封装,比如修改一些接口等等的,可以算是装修模式吧. ngOnChange 就是用这个方式去实现的哦,它已经不是内置的 life cycle hook 了.</p>
<p>我们也可以通过类似方式去加强 life cycle hook.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>4.I18n&nbsp;</p>
<p>ng 的 i18n 非常弱, 绝大部分功能从 v5 说要做到 v9 都没有实现.&nbsp;</p>
<p>v9 后已经被分离出来了,如果要使用的话需要 install package&nbsp;@angular/localize</p>
<div>之前 angular.json 里面配置 "i18nLocale":&nbsp;"en-US" 的话也会直接报错, 如果没有装 package 的话。</div>
<div>&nbsp;</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/keatkeat/p/11801621.html
頁: [1]
查看完整版本: Angular 学习笔记 (Angular 9 & ivy)