{"id":13782114,"url":"https://github.com/cicec/we-mobx","last_synced_at":"2026-03-15T02:15:15.208Z","repository":{"id":41958077,"uuid":"259915442","full_name":"cicec/we-mobx","owner":"cicec","description":":hatching_chick: We MobX - 在微信小程序中使用 MobX","archived":false,"fork":false,"pushed_at":"2023-07-11T19:49:37.000Z","size":124,"stargazers_count":12,"open_issues_count":2,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-09T18:56:29.860Z","etag":null,"topics":["javascript","miniprogram","mobx","reactive-programming","typescript","wechat-mini-program"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cicec.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-04-29T12:10:36.000Z","updated_at":"2024-05-06T04:30:26.000Z","dependencies_parsed_at":"2024-01-07T00:08:34.216Z","dependency_job_id":null,"html_url":"https://github.com/cicec/we-mobx","commit_stats":null,"previous_names":["clancysong/we-mobx"],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/cicec/we-mobx","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cicec%2Fwe-mobx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cicec%2Fwe-mobx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cicec%2Fwe-mobx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cicec%2Fwe-mobx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cicec","download_url":"https://codeload.github.com/cicec/we-mobx/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cicec%2Fwe-mobx/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273953518,"owners_count":25197316,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-06T02:00:13.247Z","response_time":2576,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","miniprogram","mobx","reactive-programming","typescript","wechat-mini-program"],"created_at":"2024-08-03T18:01:33.021Z","updated_at":"2026-03-15T02:15:10.179Z","avatar_url":"https://github.com/cicec.png","language":"TypeScript","funding_links":[],"categories":["工具"],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003eWe MobX - 在微信小程序中加入 MobX\u003c/h1\u003e\n\n\u003cdiv align=\"center\"\u003e以最简洁的调用实现微信小程序与 MobX 之间的绑定\u003c/div\u003e\n\n####\n\n\u003e MobX 是一个简单、可扩展的状态管理方案。关于 MobX 的介绍与使用请移步 [MobX](https://mobx.js.org/README.html)\n\n## ✨ 特性\n\n* 📐 简洁、统一的调用风格\n* ⚡️ 优秀的性能\n* 📎 完善的类型支持\n\n## 📦 安装\n\n```\nnpm install mobx we-mobx\n```\n\n或者\n\n```\nyarn add mobx we-mobx\n```\n\n## 📖 API\n\n#### `provider(stores)(options)`\n\n为小程序传递 stores 的全局引用，options 为 `App()` 的选项参数。\n\n```ts\nimport { provider } from 'we-mobx'\nimport stores from './store/index'\n\nconst App = provider(stores)\n\nApp\u003cIAppOption\u003e({\n  ...\n})\n```\n\n#### `observer.page(options)(observedStores)`\n\n#### `observer.component(options)(observedStores)`\n\noptions 为页面或组件的选项参数，observedStores 为需被监听的 stores 对象，调用后会完成页面和组件的声明。\n\n单独使用时需要调用 observe 完成页面声明，此时需要将 store 对象的直接引用作为参数 observedStores 传入。在 inject 中使用时则不需要，observedStores 的传递与 observe 的调用会在 inject 内部进行。\n\n```ts\nimport { observer } from 'we-mobx'\n\nconst observe = observer.page({\n  ...\n})\n\nobserve({ storeA, storeB })\n```\n\n#### `inject.page(...storeName[])(createObserver)`\n\n为页面注入需被监听的 store。调用前需要使用 provider 将 stores 全局引入。\n\ncreateObserver 的参数为 stores 的直接引用，返回值为 observer 函数调用返回的 observe 函数。此时无需再次调用 observe 函数指定监听对象，inject 会自动完成需被监听的 stores 的注入。\n\n```ts\nimport { inject } from 'we-mobx'\n\ninject\u003cStores\u003e('storeA', 'storeB')(({ storeA }) =\u003e\n  observer.page({\n    ...\n  })\n)\n```\n\n## 🏀 使用\n\n#### 定义一个 store\n\n```ts\nimport { observable, computed, action } from 'mobx'\n\nclass Todo {\n  id = Math.random()\n\n  @observable title = ''\n  @observable completed = false\n\n  constructor(title: string) {\n    this.title = title\n  }\n}\n\nclass TodoStore {\n  @observable todos: Todo[] = []\n\n  @action\n  add(title: string) {\n    this.todos.push(new Todo(title))\n  }\n\n  @action\n  remove(id: number) {\n    this.todos.splice(\n      this.todos.findIndex(todo =\u003e todo.id === id),\n      1\n    )\n  }\n\n  @action\n  toggle(id: number) {\n    const todo = this.todos.find(item =\u003e item.id === id)\n\n    if (todo) {\n      todo.completed = !todo.completed\n    }\n  }\n}\n\nexport default new TodoStore()\n```\n\n#### 传递全局 stores 并在页面中注入指定的 store\n\n```ts\nimport { provider } from 'we-mobx'\nimport { todoStore } from './store/index'\n\nconst App = provider({ todoStore })\n\nApp\u003cIAppOption\u003e({\n  ...\n})\n```\n\n```ts\nimport { inject } from 'we-mobx'\n\ninject\u003cStores\u003e('todoStore')(({ todoStore }) =\u003e\n  observer.page({\n    count: 0,\n\n    addTodo() {\n      todoStore.add('My Todo ' + ++this.count)\n    },\n\n    removeTodo(e: any) {\n      const { id } = e.currentTarget.dataset\n      todoStore.remove(id)\n    },\n\n    toggleCompleted(e: any) {\n      const { id } = e.currentTarget.dataset\n      todoStore.toggle(id)\n    },\n  })\n)\n```\n\n#### 如果只使用 observer，则需要再次调用以传入需监听的 store 对象\n\n```ts\nimport { observer } from 'we-mobx'\nimport { todoStore } from '../../store/index'\n\nconst observe = observer.page({\n  count: 0,\n\n  addTodo() {\n    todoStore.add('My Todo ' + ++this.count)\n  },\n\n  removeTodo(e: any) {\n    const { id } = e.currentTarget.dataset\n    todoStore.remove(id)\n  },\n\n  toggleCompleted(e: any) {\n    const { id } = e.currentTarget.dataset\n    todoStore.toggle(id)\n  },\n})\n\nobserve({ todoStore })\n```\n\n#### store 中的状态会被映射至 data 中，直接在 wxml 中引用\n\n```html\n\u003cview wx:for=\"{{ todoStore.todos }}\" wx:key=\"id\" data-id=\"{{ item.id }}\" bindtap=\"toggleCompleted\"\u003e\n  \u003cview\u003e\n    \u003cview\u003e\n      \u003ctext\u003e{{ item.title }}\u003c/text\u003e\n    \u003c/view\u003e\n    \u003ccheckbox checked=\"{{ item.completed }}\"\u003e\u003c/checkbox\u003e\n  \u003c/view\u003e\n\u003c/view\u003e\n\n\u003cbutton bindtap=\"addTodo\"\u003e添加\u003c/button\u003e\n```\n\n## 🌟 FAQ\n\n### 应该使用 provider \u0026 inject 的方式，还是直接使用 observer ？\n\n推荐 provider \u0026 inject 这样的引入方式。如果只想在特定的页面引入 store，或是偏好更灵活直观的调用方式，可以只使用 observer。\n\n### 在 provider 或 observer 传入的 stores 结构应该是怎样的？\n\n在 provider 中需要传入全部 stores 的引用，并且需遵循 `stores: { storeA, storeB }` 这样的格式。\n\n在 observer 中传递的结构与 provider 中类似，但只需传递需被监听的 store。\n\n## 📄 License\n\n[MIT](https://github.com/clancysong/we-mobx/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcicec%2Fwe-mobx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcicec%2Fwe-mobx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcicec%2Fwe-mobx/lists"}