当前位置:网站首页>小程序实现全局数据共享
小程序实现全局数据共享
2022-06-10 14:37:00 【SpaceX7_s】
1安装数据共享第三方包 Mobx
npm i --save mobx-[email protected]4.13.2 mobx-miniprogram-[email protected]1.2.1
注意:MobX 相关的包安装完毕之后,记得删除 miniprogram_npm 目录后,重新构建 npm。
2. 创建 MobX 的 Store 实例
在根目录下创建store文件夹,并创建store.js文件
import {
observable,action} from 'mobx-miniprogram'
export const store = observable({
//数据字段
numA:1,
numB:2,
//计算属性
get sum(){
return this.numA + this.numB
},
//actions方法,用来修改store中的数据
updateNum1:action(function(step){
this.numA += step
}),
updateNum2:action(function(step){
this.numB += step
})
})
4.在页面中使用store
//首先引入方法
import {
createStoreBindings } from "mobx-miniprogram-bindings";
import {
store} from '../../store/store'
/** * 生命周期函数--监听页面加载 */
onLoad: function (options) {
this.storeBindings= createStoreBindings(this,{
//数据源
store,
//映射变量和计算属性
fields:['numA','numB','sum'],
//映射方法
actions:['updateNum1']
})
},
/** * 生命周期函数--监听页面卸载 */
onUnload: function () {
this.storeBindings.destroyStoreBindings()
},
在页面中使用store的数据
<view>{
{
numA}}+{
{
numB}}={
{
sum}}</view>
<button type="primary" bindtap="btnclick" data-step="{
{1}}">numA+1</button>
<button type="primary" bindtap="btnclick" data-step="{
{-1}}">numA-1</button>
//在js中调用方法
btnclick(e){
this.updateNum1(e.target.dataset.step)
},
在组件中使用store
import {
storeBindingsBehavior } from "mobx-miniprogram-bindings";
import {
store } from "../../store/store";
//通过storeBindingsBehavior来实现自动绑定
behaviors: [storeBindingsBehavior],
/** * 组件的属性列表,接收页面传过来的参数 */
storeBindings: {
store,//指定要绑定的store
fields:{
numA:'numA',
numB:'numB',
sum:'sum'
},
actions: {
//指定要绑定的方法
updateNum2: 'updateNum2'
}
},
页面中使用
<view>{
{
numA}}+{
{
numB}}={
{
sum}}</view>
<button type="primary" bindtap="btnclick" data-step="{
{1}}">numA+1</button>
<button type="primary" bindtap="btnclick" data-step="{
{-1}}">numA-1</button>
//在methods中调用方法
btnclick(e){
this.updateNum2(e.target.dataset.step)
}
边栏推荐
- [solution] each time the trained model is loaded, the generated vector will be different
- 产品开发的早期阶段,是选择开发app还是小程序?
- LeetCode_21(合并两个有序链表)
- 数仓的基本概念
- 【离散数学期复习系列】六、树
- Leetcode 2293. Minimax game (yes. One pass)
- Anaconda installs opencv (CV2) and uses it in the jupyter notebook
- 作为程序员,对于底层原理真的有那么重要吗?
- Does Fortran have a standard library
- LeetCode_ 21 (merge two ordered linked lists)
猜你喜欢

【LogoDetection 数据集处理】(4)提取每张图片的logo区域

【LogoDetection 数据集处理】(3)将训练集按照类别划分为多个文件夹

几种方式可以实现 JMeter 参数化?

什么是CAS 以及 CAS 中的 ABA 问题

【离散数学期复习系列】二、一阶逻辑(谓词逻辑)

2022年危险化学品生产单位安全生产管理人员考试模拟100题及在线模拟考试
![[logodetection data set processing] (2) draw the label box of the training set picture](/img/66/6c19b80b99d1e3ce50bac439e0e903.jpg)
[logodetection data set processing] (2) draw the label box of the training set picture

Primary master-slave table integration process development process

【LogoDetection 数据集处理】(2)画出训练集图片的标注框

北京/上海内推 | 微软亚洲研究院系统与网络组招聘全职实习生
随机推荐
Does Fortran have a standard library
AUTOCAD——设置文字间距与行距
Collision detection unity experiment code
As a programmer, is it really that important for the underlying principles?
1
[discrete mathematics review series] v. some special charts
C multithreading learning note 4
c#浅拷贝与深拷贝自定义的类的List<>
【离散数学期复习系列】六、树
[advanced MySQL] optimize SQL by using the execution plan explain (2)
STM8S103f单片机的开发(1)LED灯的点亮
【重庆大学】初试复试资料分享(附考研群)
Flutter Icon Stack LIsttitle... Learning summary 3
[cloud native | kubernetes] in depth RC, RS, daemonset, statefulset (VII)
For the first time, the Pentagon admitted to funding 46 biological facilities in Ukraine. Russia once revealed that only three were safe
2022危险化学品经营单位主要负责人考试题库及在线模拟考试
焱融看|混合云环境下,如何实现数据湖最优存储解决方案
Gin blog summary 1
产品开发的早期阶段,是选择开发app还是小程序?
Leetcode 2293. 极大极小游戏(可以.一次过)