1 | # Angular 2 Hot Module Replacement
|
2 | > Angular2-HMR
|
3 |
|
4 |
|
5 | This module requires Angular 2.0.0-rc.1 or higher. Please see repository [Angular 2 Webpack Starter](https://github.com/angularclass/angular2-webpack-starter) for a working example.
|
6 |
|
7 | `main.browser.ts`
|
8 | ```typescript
|
9 | /*
|
10 | * Hot Module Reload
|
11 | * experimental version by @gdi2290
|
12 | */
|
13 | if (isDevelopment) {
|
14 | // activate hot module reload
|
15 | let ngHmr = require('angular2-hmr');
|
16 | ngHmr.hotModuleReplacement(main, module);
|
17 | } else {
|
18 | // bootstrap when document is ready
|
19 | document.addEventListener('DOMContentLoaded', () => main());
|
20 | }
|
21 | ```
|
22 | `app.service.ts`
|
23 | ```typescript
|
24 | import {HmrState} from 'angular2-hmr';
|
25 |
|
26 | export class AppState {
|
27 | // @HmrState() is used by HMR to track the state of any object during a hot module replacement
|
28 | @HmrState() _state = { };
|
29 | }
|
30 | ```
|
31 |
|
32 | `app.service.ts`
|
33 | ```typescript
|
34 | import {HmrState} from 'angular2-hmr';
|
35 | @Component({ /*... /* })
|
36 | export class App {
|
37 |
|
38 | @HmrState() localState = {};
|
39 |
|
40 | }
|
41 | ```
|