微信小程序MoxB實(shí)現(xiàn)全局狀態(tài)管理流程詳解
github 地址:https://github.com/wechat-miniprogram/mobx-miniprogram-bindings。
安裝 MobX
- 在小程序根目錄下執(zhí)行
npm install --save mobx-miniprogram mobx-miniprogram-bindings
安裝mobx-miniprogram
和mobx-miniprogram-bindings
。 - 點(diǎn)擊開(kāi)發(fā)者工具中的菜單欄:工具 --> 構(gòu)建 npm 完成構(gòu)建。
創(chuàng)建 MobX Store
在小程序根目錄下新建 store.js
文件,創(chuàng)建 MobX Store。
// store.js import { observable, action } from "mobx-miniprogram"; export const store = observable({ // 數(shù)據(jù)字段 numA: 1, numB: 2, // 計(jì)算屬性 get sum() { return this.numA + this.numB; }, // actions updateNumA: action(function () { this.numA = 3; }), });
使用 MobX Store
將頁(yè)面、自定義組件和 store 綁定有兩種方式: behavior 綁定和手工綁定 。
behavior 綁定:使用 storeBindingsBehavior 這個(gè) behavior 和 storeBindings 定義段。
import { storeBindingsBehavior } from "mobx-miniprogram-bindings"; Component({ behaviors: [storeBindingsBehavior], storeBindings: { // 綁定配置 }, // 也可以把 storeBindings 設(shè)置為一個(gè)數(shù)組,可以同時(shí)綁定多個(gè) store storeBindings: [ { // 綁定配置 1 }, { // 綁定配置 2 }, ], })
手工綁定: 使用 createStoreBindings 創(chuàng)建綁定,它會(huì)返回一個(gè)包含清理函數(shù)的對(duì)象用于取消綁定。
在頁(yè)面 onUnload 或自定義組件 detached 時(shí)一定要調(diào)用清理函數(shù),否則將導(dǎo)致內(nèi)存泄漏。
import { createStoreBindings } from "mobx-miniprogram-bindings";Page({ onLoad() { this.storeBindings = createStoreBindings(this, { // 綁定配置 }); }, onUnload() { this.storeBindings.destroyStoreBindings(); },});import { createStoreBindings } from "mobx-miniprogram-bindings"; Page({ onLoad() { this.storeBindings = createStoreBindings(this, { // 綁定配置 }); }, onUnload() { this.storeBindings.destroyStoreBindings(); }, });
無(wú)論使用哪種綁定方式,都必須提供一個(gè)綁定配置對(duì)象。這個(gè)對(duì)象包含以下字段:
store:一個(gè) MobX observable。用于指定默認(rèn)的 MobX store。
fields:數(shù)組或?qū)ο?。用于指定需要綁定?data 字段。
fields 有三種形式:
- 數(shù)組形式:指定 data 中哪些字段來(lái)源于 store 。例如
['numA', 'numB']
。 - 映射形式:指定 data 中哪些字段來(lái)源于 store 以及它們?cè)?store 中對(duì)應(yīng)的名字。例如
{ a: 'numA', b: 'numB' }
,此時(shí)this.data.a === store.numA
,this.data.b === store.numB
。 - 函數(shù)形式:指定 data 中每個(gè)字段的計(jì)算方法。例如
{ a: () => store.numA, b: () => anotherStore.numB }
,此時(shí)this.data.a === store.numA
,this.data.b === anotherStore.numB
。
actions:數(shù)組或?qū)ο蟆S糜谥付ㄐ枰成涞?actions,將 store 中的一些 actions 放入頁(yè)面或自定義組件的 this 下。
actions 有兩種形式:
- 數(shù)組形式:例如
['update']
,此時(shí)this.update === store.update
。 - 映射形式:例如
{ buttonTap: 'update' }
,此時(shí)this.buttonTap === store.update
。
為了提升性能,在 store 中的字段被更新后,并不會(huì)立刻同步更新到 this.data 上,而是等到下個(gè) wx.nextTick 調(diào)用時(shí)才更新,這樣可以顯著減少 setData 的調(diào)用次數(shù)。
如果需要立刻更新,可以在 behavior 綁定中調(diào)用 this.updateStoreBindings()
,或者在手工綁定中調(diào)用 this.storeBindings.updateStoreBindings()
。
如果只是更新對(duì)象中的一部分,是不會(huì)引發(fā)界面變化的。
例如:this.someObject.someField = "xxx";
是不會(huì)觸發(fā)界面更新的,可以改成this.someObject = Object.assign({}, this.someObject, { someField: "xxx" });
。
在 Component 構(gòu)造器中使用
import { storeBindingsBehavior } from "mobx-miniprogram-bindings"; import { store } from "../../store/store"; Component({ behaviors: [storeBindingsBehavior], storeBindings: { store, fields: { numA: "numA", numB: "numB", sum: "sum", }, actions: { updateNumA: "updateNumA", }, }, methods: { myMethod() { this.updateNumA() wx.nextTick(() => { const A = this.data.numA; // 3 const B = this.data.numB; // 2 const C = this.data.sum; // 5 }) }, }, });
在 Page 頁(yè)面中使用
小程序基礎(chǔ)庫(kù)版本 2.9.2 以上,可以直接像 Component 構(gòu)造器那樣引入 behaviors 。
import { createStoreBindings } from "mobx-miniprogram-bindings"; import { store } from "../../store/store"; Page({ onLoad() { this.storeBindings = createStoreBindings(this, { store, fields: ["numA", "numB", "sum"], actions: ["updateNumA"], }); wx.nextTick(() => { const A = this.data.numA; // 1 const B = this.data.numB; // 2 const C = this.data.sum; // 3 }) }, onUnload() { this.storeBindings.destroyStoreBindings(); }, myMethod() { this.updateNumA() wx.nextTick(() => { const A = this.data.numA; // 3 const B = this.data.numB; // 2 const C = this.data.sum; // 5 }) }, })
到此這篇關(guān)于微信小程序MoxB實(shí)現(xiàn)全局狀態(tài)管理流程詳解的文章就介紹到這了,更多相關(guān)小程序MoxB全局狀態(tài)管理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- uniapp實(shí)現(xiàn)h5、app與微信小程序三端pdf文件下載和預(yù)覽功能
- 微信小程序開(kāi)發(fā)自定義tabBar實(shí)戰(zhàn)案例(定制消息99+小紅心)
- 微信小程序獲取當(dāng)前位置的詳細(xì)步驟
- 微信小程序怎么加入JavaScript腳本,做出動(dòng)態(tài)效果
- 微信小程序如何設(shè)置基本的頁(yè)面樣式,做出用戶(hù)界面UI
- 制作微信小程序的小白簡(jiǎn)單入門(mén)教程
- 微信小程序?qū)崿F(xiàn)云開(kāi)發(fā)上傳文件、圖片功能
- 微信小程序獲取用戶(hù)頭像昵稱(chēng)組件封裝實(shí)例(最新版)
- 微信小程序父子組件通信詳細(xì)介紹
- 微信小程序常用功能實(shí)例匯總包括上拉刷新,下拉加載,列表數(shù)據(jù)綁定,輪播,參數(shù)傳遞
相關(guān)文章
JS基于Location實(shí)現(xiàn)訪問(wèn)Url、重定向及刷新頁(yè)面的方法分析
這篇文章主要介紹了JS基于Location實(shí)現(xiàn)訪問(wèn)Url、重定向及刷新頁(yè)面的方法,結(jié)合實(shí)例形式分析了javascript使用Location進(jìn)行URL訪問(wèn)、重定向、頁(yè)面刷新等操作相關(guān)原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-12-12Easy.Ajax 部分源代碼 支持文件上傳功能, 兼容所有主流瀏覽器
下面是Easy.Ajax類(lèi)的初稿,如須發(fā)表,在代碼上還要修改以達(dá)到最簡(jiǎn),但API是不會(huì)變了2011-02-02js實(shí)現(xiàn)課堂隨機(jī)點(diǎn)名系統(tǒng)
這篇文章主要介紹了js實(shí)現(xiàn)課堂隨機(jī)點(diǎn)名系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11JavaScript對(duì)數(shù)字的判斷與處理實(shí)例分析
這篇文章主要介紹了JavaScript對(duì)數(shù)字的判斷與處理方法,實(shí)例分析了javascript判斷數(shù)字的常見(jiàn)方法與針對(duì)數(shù)字處理的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02微信小程序canvas繪制圓角base64圖片的實(shí)現(xiàn)
這篇文章主要介紹了微信小程序canvas繪制圓角base64圖片的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08JavaScript使用canvas繪制隨機(jī)驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了JavaScript使用canvas繪制隨機(jī)驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02