【微信小程序】Mobx--绑定多个 store 以及命名空间

在实际开发中,一个页面或者组件可能会绑定多个 Store ,这时候我们可以将 storeBindings 改造成数组。数组每一项就是一个个要绑定的 Store

如果多个 Store 中存在相同的数据,显示会出现异常。还可以通过 namespace 属性给当前 Store 开启命名空间,在开启命名空间以后,访问数据的时候,需要加上 namespace 的名字才可以

// behavior.js

import {
    
     BehaviorWithStore } from 'mobx-miniprogram-bindings'
import {
    
     numStore } from '../../stores/numstore'

export const indexBehavior = BehaviorWithStore({
    
    
  storeBindings: [
    {
    
    
      namespace: 'numStore',
      store: numStore,
      fields: ['numA', 'numB', 'sum'],
      actions: ['update'],
    }
  ]
})


// index/index.wxml
<view>{
    
    {
    
     numStore.numA }} + {
    
    {
    
     numStore.numB }} = {
    
    {
    
    numStore.sum}}</view>


摘录:https://blog.csdn.net/qq_63358859/article/details/136347834

猜你喜欢

转载自blog.csdn.net/weixin_40874076/article/details/141702465