
MobX integration for Vue.js, inspired by vue-rx.
Movue aims for providing simple and reliable integration between Mobx and Vue.js, which sometimes means less convenience. You may want to try mobx-vue if you are facing more complex situation. :)
Why movue
Why MobX + movue, instead of Vuex?
Install
npm i movue --save
If you use yarn,
yarn add movue
Usage
Import Movue in your project and use it in Vue:
Vue
You can pass the min parts of MobX to reduce bundle size:
Vue
Now you can use data from MobX store in your Vue component:
// given MobX storeconst todoStore = // given vue component {/* ... */} computed: /* ... */ // you should use data from MobX store only in `fromMobx` properties fromMobx: { return todoStoreunfinishedTodos } methods: { todoStore }
Properties defined in fromMobx
can be used in the template or other parts of viewModel just like normal Vue computed
properties:
Count of unfinished todos: {{unfinishedTodos.length}}
Like computed
properties, we can define getter & setter for fromMobx
properties:
fromMobx: todos: // getter { return todoStoretodos } // setter { todoStore }
You can use helper methods to simplify your code:
fromMobx: methods: // `...` requires object spread syntax support ... {/* ... */}
movue works well with vue-class-component:
@ // get todos @FromMobx { return todoStoretodos } // you don't need decorator for setters { todoStore } // you can also set value with a component method { todoStore }
API Reference
mapFields(store: object, fieldNames: string[]): Object
mapFields
do fields' map for you:
const fields = // equalsconst fields = { return todoStoretodos } { return todoStoreunfinishedTodos }
mapFields(store: object, fieldNames: {[fieldAlias: string]: string}): Object
You can use aliases for fields:
const fields = // equalsconst fields = { return todoStoretodos } { return todoStoreunfinishedTodos }
mapFields(store: object, fieldNames: {[fieldAlias: string]: { get: string, set?: string }}): Object
Also you can specify a setter for the field:
const fields = // equalsconst fields = { return todoStoretodos } { return todoStoreunfinishedTodos } newTodoItemName: { return todoStorenewItemName } { todoStore }
mapFields(store: object, fieldNames: {[fieldAlias: string]: { get: (store: object) => any, set?: (store: object, value: any) => void }}): Object
You can specify a complex setter and getter for the field:
const fields = // equalsconst fields = { return todoStoretodos } { return todoStoreunfinishedTodos } newTodoItemName: { return todoStorenewItemName } { todoStore }
mapMethods(store: object, methodNames: string[]): Object
mapMethods
do methods' map for you:
const methods = // equalsconst methods = addTodo: todoStoreaddTodo toggleTodo: todoStoretoggleTodo
mapMethods(store: object, methodNames: {[methodAlias: string]: string}): Object
You can use aliases for methods:
const methods = // equalsconst methods = addTodoItem: todoStoreaddTodo checkTodoItem: todoStoretoggleTodo
FromMobx(target: Vue, key: string): void
FromMobx
helps to use movue together with vue-class-component. You should use FromMobx
as decorator for class property accessors:
@ @FromMobx { return todoStoretodos }