当前位置:网站首页>Mini Program - Global Data Sharing
Mini Program - Global Data Sharing
2022-07-31 00:32:00 【like feynman%】
1. What is global data sharing
Global data sharing (also known as: state management) is to solve the problem of data sharing between components.Common global data sharing schemes in development include: Vuex, Redux, Mobx, etc. 
2. Global data sharing scheme in applet
You can use mobx-miniprogram with mobx-miniprogram-bindings to achieve global data sharing.of which
- modx-miniprogram is used to create a Store instance object
- mobx-miniprogram-bindings are used to Bind the shared data or methods in the Store to components or pages for use
3. Install Mobx package

Note: After installing Mobx-related packages, remember to delete the miniprogram-npm directory and re-build npm
4. Create Mobx Store instance

//In this js file, it is specially used to create an instance object of Storeimport {observable} from 'mobx-miniprogram'export const store = observable({//data fieldnumA:1,numB:2,//computed propertygetsum(){return this.numA+this.numB},//actions method, used to modify the data in the storeupdataNum1:action(function (step){ this.numA += step}),updataNum2:action(function (step){ this.numB += step}),})5. Bind the members in the store to the page
//Page jsimport { createStoreBindings } from 'mobx-miniprogram-bindings'import { store } from '../../store/store'onLoad: function (options) {this.storeBindings = createStoreBindings(this,{store,//data sourcefields:['numA','numB','sum'],actions:['updateNum1']})},onUnload: function () {//Uninstall this storethis.storeBindings.destroyStoreBindings()},//Page wxml{{numA}}+{{numB}} = {{sum}} numA+1 numA-1 //page jsbtnHandler1(e){// console. log(e)this.updateNum1(e.target.dataset.step)},6. Bind the store member to the component
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'import { store } from '../../store/store'Component({/*** Component property list*/behaviors:[storeBindingsBehavior],//Automatic binding through storeBindingsBehavior//I don't know where storeBindings came fromstoreBindings:{store,fields:{//fields cannot be written incorrectly, otherwise it will not rendernumA:()=>store.numA,//The first way to bindnumB:(store)=>store.numB,//The second way of bindingSum:'sum'//The third way of binding},actions:{updateNum2:'updateNum2'}},})Using members from Store in components
//Group price.wxml structure{{numA}}+{{numB}}={{sum}} numB+1 numB-1 //List of methods of the componentmethods: {btnHandler2(e){this.updateNum2(e.target.dataset.step)}}边栏推荐
- MySQL数据库约束,表的设计
- joiplay模拟器如何使用
- Jetpack Compose学习(8)——State及remeber
- An easy-to-use interface testing tools - the Postman
- 加密传输过程
- mysql主从复制及读写分离脚本-亲测可用
- A complete guide to avoiding pitfalls for the time-date type "yyyy-MM-dd HHmmss" in ES
- [In-depth and easy-to-follow FPGA learning 15---------- Timing analysis basics]
- GO GOPROXY proxy Settings
- A Brief Talk About MPI
猜你喜欢

从笔试包装类型的11个常见判断是否相等的例子理解:包装类型、自动装箱与拆箱的原理、装箱拆箱的发生时机、包装类型的常量池技术

joiplay模拟器如何调中文

Error occurred while trying to proxy request The project suddenly can't get up

joiplay模拟器如何导入游戏存档

C语言力扣第48题之旋转图像。辅助数组

unity2D横版游戏教程4-物品收集以及物理材质

MySQL数据库(基础)

xss靶机训练【实现弹窗即成功】

【深入浅出玩转FPGA学习13-----------测试用例设计1】

After writing business code for many years, I found these 11 doorways, which only experts know
随机推荐
【愚公系列】2022年07月 Go教学课程 017-分支结构之IF
WEB安全基础 - - -漏洞扫描器
MySQL中substring与substr区别
ELK部署脚本---亲测可用
ABC 261 F - Sorting Color Balls(逆序对)
joiplay模拟器不支持此游戏类型怎么解决
分布式系统的一致性与共识(1)-综述
Error occurred while trying to proxy request The project suddenly can't get up
【c语言课程设计】C语言校园卡管理系统
【愚公系列】2022年07月 Go教学课程 013-常量、指针
牛客网刷题训练(四)
[Tang Yudi Deep Learning-3D Point Cloud Combat Series] Study Notes
pytorch双线性插值
MySQL数据库(基础)
binglog log tracking: data backup and backup tracking
Restricted character bypass
An easy-to-use interface testing tools - the Postman
Point Cloud Scene Reconstruction with Depth Estimation
Installation considerations for pytorch
Regular expression password policy and regular backtracking mechanism bypass