当前位置:网站首页>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)}}
边栏推荐
- 【愚公系列】2022年07月 Go教学课程 017-分支结构之IF
- WEB安全基础 - - -漏洞扫描器
- 46.
- A Brief Talk About MPI
- 【Yugong Series】July 2022 Go Teaching Course 017-IF of Branch Structure
- Shell programming conditional statement test command Integer value, string comparison Logical test File test
- WMware Tools installation failed segmentation fault solution
- Niuke.com question brushing training (4)
- Mysql体系化之JOIN运算实例分析
- [In-depth and easy-to-follow FPGA learning 15---------- Timing analysis basics]
猜你喜欢
An easy-to-use interface testing tools - the Postman
SWM32系列教程6-Systick和PWM
Kotlin协程:协程上下文与上下文元素
After writing business code for many years, I found these 11 doorways, which only experts know
xss的绕过
正则表达式密码策略与正则回溯机制绕过
Jmeter参数传递方式(token传递,接口关联等)
joiplay模拟器报错如何解决
MySQL database advanced articles
【c语言课程设计】C语言校园卡管理系统
随机推荐
xss靶机训练【实现弹窗即成功】
【愚公系列】2022年07月 Go教学课程 017-分支结构之IF
WEB安全基础 - - -漏洞扫描器
encrypted transmission process
The difference between h264 and h265 decoding
[Deep learning] Detailed explanation of Transformer model
The difference between truncate and delete in MySQL database
pytorch双线性插值
How to install joiplay emulator rtp
Gabor滤波器学习笔记
binglog log tracking: data backup and backup tracking
XSS相关知识
asser利用蚁剑登录
【深度学习】Transformer模型详解
Consistency and Consensus of Distributed Systems (1) - Overview
[Yugong Series] July 2022 Go Teaching Course 015-Assignment Operators and Relational Operators of Operators
软件开发设计流程
Summary of the stock problem of state machine dynamic programming
【Multithreading】
Redis learning