Skip to content

Conversation

brunolm
Copy link
Owner

@brunolm brunolm commented Jun 22, 2018

Add on your module

import { MobxAngularModule } from 'mobx-angular';

@NgModule({
  ...
  imports: [
    ...
    MobxAngularModule,
    ...
  ],
  providers: [AppStoreService, AboutStoreService, ContactStoreService, HeroesStoreService, TutorialsStoreService],
  ...
})
export class AppModule {}

Store example

import { Injectable } from '@angular/core';
import { action, observable } from 'mobx-angular';

@Injectable({
  providedIn: 'root',
})
export class AboutStoreService {
  @observable title: string;

  constructor() {
    this.initialize();
  }

  @action
  private initialize() {
    this.title = 'about:hello world';
  }

  @action
  randomTitle() {
    // tslint:disable-next-line:no-magic-numbers
    this.title = Math.random().toString(32);
  }
}

App store (redux like)

import { Injectable } from '@angular/core';

import { AboutStoreService } from '../about/about-store.service';
import { ContactStoreService } from '../contact/contact-store.service';
import { HeroesStoreService } from '../heroes/heroes-store.service';
import { TutorialsStoreService } from '../tutorials/tutorials-store.service';

@Injectable({
  providedIn: 'root',
})
export class AppStoreService {
  constructor(
    public about: AboutStoreService,
    public contact: ContactStoreService,
    public heroes: HeroesStoreService,
    public tutorials: TutorialsStoreService,
  ) {}
}

App component

import { configure } from 'mobx';

constructor() {
  configure({ enforceActions: 'strict' });
}

@coveralls
Copy link

coveralls commented Jun 22, 2018

Coverage Status

Coverage increased (+1.5%) to 83.824% when pulling 8b0347c on feature/mobx into 49bd7a0 on master.

@brunolm brunolm merged commit 23b3048 into master Jun 22, 2018
@brunolm brunolm deleted the feature/mobx branch June 22, 2018 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants