UNPKG

4.92 kBMarkdownView Raw
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)
6If you're looking for the Angular 1 version version, it's [here](https://github.com/NgMobx/ng1-mobx)
7
8## Features
91. The library allows you to automatically observe all the observables that your component uses
102. Automatically runs change detection - works great with OnPush strategy
113. Disposes of all the observers when the component is destroyed
124. Debugging tools
13
14## Usage
15
16Install:
17```
18$ npm install --save mobx-angular mobx
19```
20
21Import the MobxAngularModule:
22```ts
23import { MobxAngularModule } from 'mobx-angular';
24
25@NgModule({
26 imports: [..., MobxAngularModule]
27})
28export class MyModule {}
29```
30
31## autorun
32Use `*mobxAutorun` directive in your template:
33```ts
34import { Component, ChangeDetectionStrategy } from '@angular/core';
35import {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})
46export class AppComponent {
47 store = store;
48}
49```
50
51The 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
56Under the hood, this magic happens by running `autorun(() => view.detecChanges)`
57
58## dontDetach (and Upgrading from mobx-angular 1.X to 2.X)
59Notice that in 2.X the *default* behaviour of *mobxAutorun is to detach from Change Detection.
60
61If 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
67This method is deprecated - do not use it
68
69## reaction
70Aside from autorun, MobX allows you to react to specific data changes.
71
72Usage:
73```ts
74import { Component, ChangeDetectionStrategy } from '@angular/core';
75
76@Component({
77 changeDetection: ChangeDetectionStrategy.OnPush,
78 template: `<div *mobxReaction="getParity.bind(this)">
79 {{ parity }}
80 </div>`
81})
82class AppComponent {
83 getParity() {
84 return this.parity = store.counter % 2 ? 'Odd' : 'Even';
85 }
86}
87```
88The `callback` function will automatically re-run whenever any observable that it uses changes.
89Change Detection will run automatically whenever the return value of `callback` changes.
90If you don't return anything, change detection will not run.
91
92In this example, the `parity` property will be updated according to `counter`,
93and change detection will run only when the `parity` changes.
94
95## Injectable stores
96You can easily make your stores injectable:
97```ts
98import { observable, action } from 'mobx-angular';
99
100@Injectable()
101class Store {
102 @observable value;
103 @action doSomething() { ... }
104}
105```
106## Using with OnPush or ngZone: 'noop'
107To achieve great performance, you can set `OnPush` change detection strategy on your components (this can be configured as default in `.angular-cli.json`).
108MobX 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').
111Please 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
114mobx-angular comes with a handy debug tool.
115To turn on / off the debug tool, open developer tools' console, and run:
116```ts
117mobxAngularDebug(true) // turn on
118mobxAngularDebug(false) // turn off
119```
120Then you can right-click on the components that use mobx directives, and you will see a console log of the components' dependencies.
121Also, every action that happens in mobx will be console.logged in a nice way.
122
123## AoT
124Some 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'`
125The only thing you can't do when importing from mobx-angular is using the modifiers, such as `@observable.ref`.
126
127## Examples
128See the `example` folder, specifically these files:
129`/example/todos/src/app/stores/todos.ts`
130`/example/todos/src/app/app.component.ts`
131
132To 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```