1 | [![Build Status](https://travis-ci.org/mobxjs/mobx-angular.svg?branch=master)](https://travis-ci.org/mobxjs/mobx-angular)
|
2 | [![npm version](https://badge.fury.io/js/mobx-angular.svg)](https://badge.fury.io/js/mobx-angular)
|
3 | # mobx-angular
|
4 |
|
5 | ## MobX connector for Angular (versions 2, 4, 5)
|
6 | If you're looking for the Angular 1 version version, it's [here](https://github.com/NgMobx/ng1-mobx)
|
7 |
|
8 | ## Features
|
9 | 1. The library allows you to automatically observe all the observables that your component uses
|
10 | 2. Automatically runs change detection - works great with OnPush strategy
|
11 | 3. Disposes of all the observers when the component is destroyed
|
12 | 4. Debugging tools
|
13 |
|
14 | ## Usage
|
15 |
|
16 | Install:
|
17 | ```
|
18 | $ npm install --save mobx-angular mobx
|
19 | ```
|
20 |
|
21 | Import the MobxAngularModule:
|
22 | ```ts
|
23 | import { MobxAngularModule } from 'mobx-angular';
|
24 |
|
25 | @NgModule({
|
26 | imports: [..., MobxAngularModule]
|
27 | })
|
28 | export class MyModule {}
|
29 | ```
|
30 |
|
31 | ## autorun
|
32 | Use `*mobxAutorun` directive in your template:
|
33 | ```ts
|
34 | import { Component, ChangeDetectionStrategy } from '@angular/core';
|
35 | import {store} from './store/counter';
|
36 |
|
37 | @Component({
|
38 | changeDetection: ChangeDetectionStrategy.OnPush,
|
39 | template: `
|
40 | <div *mobxAutorun>
|
41 | {{ store.value }} - {{ store.computedValue }}
|
42 | <button (click)="store.action">Action</button>
|
43 | </div>
|
44 | `
|
45 | })
|
46 | export class AppComponent {
|
47 | store = store;
|
48 | }
|
49 | ```
|
50 |
|
51 | The directive will do the following:
|
52 | - Run `detach` on the view under *mobxAutorun (disables change detection)
|
53 | - Observe all the observables / computed values that your component uses
|
54 | - Automatically run the `detectChanges` method whenever there's a relevant change
|
55 |
|
56 | Under the hood, this magic happens by running `autorun(() => view.detecChanges)`
|
57 |
|
58 | ## dontDetach (and Upgrading from mobx-angular 1.X to 2.X)
|
59 | Notice that in 2.X the *default* behaviour of *mobxAutorun is to detach from Change Detection.
|
60 |
|
61 | If things stop working you have 3 options:
|
62 | - Define local component properties as observables or computed values
|
63 | - Surround with *mobxAutorun only the parts that actually use observable / computed values from the store
|
64 | - Disabling detach by specifying: `*mobxAutorun="{ dontDetach: true }">`. To retain good performance you can use OnPush CD strategy on the component
|
65 |
|
66 | ## autorunSync
|
67 | This method is deprecated - do not use it
|
68 |
|
69 | ## reaction
|
70 | Aside from autorun, MobX allows you to react to specific data changes.
|
71 |
|
72 | Usage:
|
73 | ```ts
|
74 | import { Component, ChangeDetectionStrategy } from '@angular/core';
|
75 |
|
76 | @Component({
|
77 | changeDetection: ChangeDetectionStrategy.OnPush,
|
78 | template: `<div *mobxReaction="getParity.bind(this)">
|
79 | {{ parity }}
|
80 | </div>`
|
81 | })
|
82 | class AppComponent {
|
83 | getParity() {
|
84 | return this.parity = store.counter % 2 ? 'Odd' : 'Even';
|
85 | }
|
86 | }
|
87 | ```
|
88 | The `callback` function will automatically re-run whenever any observable that it uses changes.
|
89 | Change Detection will run automatically whenever the return value of `callback` changes.
|
90 | If you don't return anything, change detection will not run.
|
91 |
|
92 | In this example, the `parity` property will be updated according to `counter`,
|
93 | and change detection will run only when the `parity` changes.
|
94 |
|
95 | ## Injectable stores
|
96 | You can easily make your stores injectable:
|
97 | ```ts
|
98 | import { observable, action } from 'mobx-angular';
|
99 |
|
100 | @Injectable()
|
101 | class Store {
|
102 | @observable value;
|
103 | @action doSomething() { ... }
|
104 | }
|
105 | ```
|
106 | ## Using with OnPush or ngZone: 'noop'
|
107 | To achieve great performance, you can set `OnPush` change detection strategy on your components (this can be configured as default in `.angular-cli.json`).
|
108 | MobX will run change detection manually for you on the components that need to be updated.
|
109 |
|
110 | * In Angular 5 there's a new option, which is to disable Zone completely when bootstrapping the app (ngZone: 'noop').
|
111 | Please note that this means that all 3rd-party components will stop working (because they rely on change detection to work via Zone).
|
112 |
|
113 | ## Debugging MobX
|
114 | mobx-angular comes with a handy debug tool.
|
115 | To turn on / off the debug tool, open developer tools' console, and run:
|
116 | ```ts
|
117 | mobxAngularDebug(true) // turn on
|
118 | mobxAngularDebug(false) // turn off
|
119 | ```
|
120 | Then you can right-click on the components that use mobx directives, and you will see a console log of the components' dependencies.
|
121 | Also, every action that happens in mobx will be console.logged in a nice way.
|
122 |
|
123 | ## AoT
|
124 | Some people complained about AoT when using mobx decorators inside components. In case you do that - we export a proxy to the decorators from mobx-angular, which should be AoT compatible, e.g. `import { observable, computed } from 'mobx-angular'`
|
125 | The only thing you can't do when importing from mobx-angular is using the modifiers, such as `@observable.ref`.
|
126 |
|
127 | ## Examples
|
128 | See the `example` folder, specifically these files:
|
129 | `/example/todos/src/app/stores/todos.ts`
|
130 | `/example/todos/src/app/app.component.ts`
|
131 |
|
132 | To run the examples, clone this repo and run:
|
133 | ```
|
134 | $ npm install -g @angular/cli
|
135 | $ cd example/<example-folder>
|
136 | $ npm install
|
137 | $ ng serve
|
138 | ```
|