Angular 2 / 4 / 5 - Ahead-of-time compilation how to

前端 未结 2 1813
梦毁少年i
梦毁少年i 2020-12-02 10:09

I\'m trying to bootstrap my Angular 2 RC5 application following this guide https://angular.io/docs/ts/latest/guide/ngmodule.html Below is my code

import { A         


        
2条回答
  •  难免孤独
    2020-12-02 10:27

    Both the JIT and AOT compilers generate an AppModuleNgFactory class from the same AppModule source code.

    The JIT compiler creates that factory class on the fly, in memory, in the browser. The AOT compiler outputs the factory to a physical file that we're importing here in the static version of main.ts

    At a high level, @angular/compiler-cli provides a wrapper around Typescript’s tsc compiler, and both AoT compiles your application’s code, and then transpiles your application’s Typescript to Javascript:

    $ ngc -p src
    

    This generates a new file for each component and module ( called an NgFactory )

    To run your app in AoT mode, all that’s required is changing your main.ts file from

    import {platformBrowserDynamic} from ‘@angular/platform-browser-dynamic’
    import {MyAppModule} from ‘./app’
    platformBrowserDynamic().bootstrapModule(MyAppModule);
    

    to

    import {platformBrowser} from ‘@angular/platform-browser’
    import {MyAppModuleNgFactory} from ‘./app.ngfactory’ //generated code
    platformBrowser().bootstrapModuleFactory(MyAppModuleNgFactory);
    

    EDIT:

    To use ngc command, first install these-

    $ npm install @angular/compiler-cli typescript@next @angular/platform-server @angular/compiler
    

    ngc is a drop-in replacement for tsc which you will find it in- ./node_modules/.bin/ngc folder.

提交回复
热议问题