当前位置:网站首页>小程序实现全局数据共享
小程序实现全局数据共享
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)
}
边栏推荐
- Ue5 Comment convertir les coordonnées de l'écran en coordonnées du monde et en direction du monde
- Do you understand all these difficult memory problems?
- As a programmer, is it really that important for the underlying principles?
- 博主自白
- 数据湖(六):Hudi与Flink整合
- [registration] to solve the core concerns of technology entrepreneurs, the online enrollment of "nebula plan open class" was opened
- 初试c语言之第二次笔记
- what‘t the meaning of “de facto“
- 还在说大学排名是笑话?最新规定:世界top50大学可以直接落户上海!
- At the early stage of product development, do you choose to develop apps or applets?
猜你喜欢

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

Docker deploys a redis cluster

2022 practice questions and online simulation test for the third batch of Guangdong Provincial Safety Officer a certificate (principal)

利用 GDB 快速阅读 postgresql 的内核代码

UE5如何将屏幕坐标转为世界坐标和世界方向

【离散数学期复习系列】六、树

列表、数组和张量之间的相互转化

SIGIR 2022 | HKU and Wuhan University put forward kgcl: a recommendation system based on knowledge map comparative learning

2022 the 14th Nanjing International artificial intelligence product exhibition

Flutter Icon Stack LIsttitle... Learning summary 3
随机推荐
[discrete mathematics review series] VI. tree
如何写一个全局的 Notice 组件?
初试c语言之第二次笔记
【LogoDetection 数据集处理】(2)画出训练集图片的标注框
.NET C#基础(7):接口 - 人如何和猫互动
KaTeX问题 —— csdn编辑时中打出等号对齐的样式
SIGIR 2022 | 港大、武大提出KGCL:基于知识图谱对比学习的推荐系统
My first go program
列表、数组和张量之间的相互转化
Meta公司新探索 | 利用Alluxio数据缓存降低Presto延迟
2022危险化学品经营单位主要负责人考试题库及在线模拟考试
[solution] each time the trained model is loaded, the generated vector will be different
Ue5 how to convert screen coordinates to world coordinates and World Directions
【Vue/js】通过localStorage浏览器实现变量和对象的本地缓存(图文+完整源代码)
CVPR 2022 Oral | SCI:实现快速、灵活与稳健的低光照图像增强
【原创】POI 5.x XSSF和HSSF使用自定义字体颜色
[logodetection dataset processing] (4) extract the logo area of each picture
Flutter drawer learning summary 6
Flutter listview, column, row learning personal summary 2
Gin blog summary 1