Go to Production
Check code at: https://github.com/wghglory/angular2-fundamental
Tslint
1 | npm i typescript -g |
List all errors:
1 | node_modules/.bin/tslint "app/**/*.ts" |
1 | ERROR: app/user/user.module.ts[8, 64]: Missing semicolon |
Fix all errors:
1 | node_modules/.bin/tslint "app/**/*.ts" --fix |
Tuning Your rxJS Requests
Now when running the project, you can see lots of useless requests involved Observable.js like NeverObservable.js, PairObservable.js, etc
voter.service.ts and other file, use Observable instead of Rx library
1 | - import { Observable } from 'rxjs/Rx'; |
Now in auth.service.ts, some operation is not available (do, of, map)
1 | return this.http.post('/api/login', JSON.stringify(loginInfo), options) |
So we create rxjs-extension.ts
1 | import 'rxjs/add/observable/of'; |
Now import this file to app.module.ts, afte this, “do, of, map” in auth.service.ts should not have errors
1 | import './rxjs-extensions'; |
Refresh browser and you can see requests number is much lesser.
Enabling Production Mode
Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
main.ts: based on environment.js, enableProdMode
1 | import { enableProdMode } from '@angular/core'; |
Ahead of Time Compiler Overview
AOT benefits:
- faster rendering
- few requests
- smaller angular framework
- detect template errors
- better security
JIT vs AOT:
When compiling Templates in development, angular use Just in time (JIT) compiler,
much of angular needed in browser is the complier.
It takes time to compile templates.
Production uses Ahead of time compiler
AOT no-no’s
- form.controls.controlName
- control.errors?.someError
- default exports
- functions in providers, routes or declarations of a module
- any filed used in a template, including inputs, must be public
- declare var for globals
ES6 module tree
main.ts –> app.module.ts –> many component.ts, service.ts
Preparing for the AOT Compiler
1 | npm i @angular/compiler-cli @angular/platform-server |
- create tsconfig-aot.json
- create main-aot.ts
- note now index.html is new aot html, index-jit is old one
Making Coding Fixes for the AOT Compiler
create-event.component.ts now templateUrl is full path, AOT has to use relative path.
1 | @Component({ |
update as below if using systemjs:
1 | @Component({ |
update as below if using webpack:
1 | @Component({ |
handle 3rd party library:
app.module.ts
1 | - declare let toastr: Toastr |