当前位置:网站首页>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)}}边栏推荐
- 【Multithreading】
- MySQL grant statements
- Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv
- An easy-to-use interface testing tools - the Postman
- MySQL database advanced articles
- Adding, deleting, modifying and checking the foundation of MySQL
- 什么是Promise?Promise的原理是什么?Promise怎么用?
- How to solve the error of joiplay simulator
- Jetpack Compose学习(8)——State及remeber
- Neural Network (ANN)
猜你喜欢

registers (assembly language)

Restricted character bypass

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

MySQL的grant语句

Asser uses ant sword to log in

Linux 部署mysql 5.7全程跟踪 完整步骤 django部署

Regular expression password policy and regular backtracking mechanism bypass

joiplay模拟器rtp如何安装

In-depth understanding of the auto-increment operator from two error-prone written test questions

joiplay模拟器不支持此游戏类型怎么解决
随机推荐
Add text watermark to PHP image
MySQL数据库(基础)
Go 学习笔记(84)— Go 项目目录结构
redis学习
【愚公系列】2022年07月 Go教学课程 019-循环结构之for
MySQL的grant语句
Gabor filter study notes
go mode tidy出现报错go warning “all“ matched no packages
SereTOD2022 Track2 Code Analysis - Task-based Dialogue Systems Challenge for Semi-Supervised and Reinforcement Learning
Point Cloud Scene Reconstruction with Depth Estimation
MySQL系列一:账号管理与引擎
C language force buckles the rotating image of the 48th question.auxiliary array
分布式系统的一致性与共识(1)-综述
How to ensure the consistency of database and cache data?
In-depth understanding of the auto-increment operator from two error-prone written test questions
Regular expression password policy and regular backtracking mechanism bypass
寄存器(汇编语言)
ABC 261 F - Sorting Color Balls(逆序对)
(五)fastai应用
After writing business code for many years, I found these 11 doorways, which only experts know