基于Mobx的多页面小程序的全局共享状态管理实践

what

  • 名字很长很绕靠口,总的来讲,本文是对开发小程序过程当中使用mobx的一个总结。前端

  • 状态管理,相比你们也很熟悉,顾名思义,是对前端页面繁复的状态进行管理,在此,我也不过多赘述。vue

  • 因此虽然是是用在小程序上,不过我想对于WebApp的状态管理,也有这么一丢丢启发。react

why

  • 为何要进行状态管理?
    如今的小程序俨然是Hybrid App,又像是PWA,但固然也是一个WebApp,更不用说他的语法和vue略微有这么一丢丢类似。有reactvue的实践在前,因此对于小程序上那么多的页面状态和数据缓存,势必也要引入一个状态管理工具git

  • 为何是mobx
    方便,快捷,学习成本低,固然也是仁者见仁智者见智github

how

  1. 在小程序中引入mobx
    在这里我使用了wechat-weapp-mobx这个库。在./libs目录下放入mobx.jsobserver.js这两个库,同时在./store目录下新建store.js用于存放全局状态。小程序

  2. 创建store
    因为小程序中不支持@decorate装饰器,因此采用了extendObservable的写法。另外,小程序支持import语法和require语法。我比较喜欢import语法,大家呢?我认为在action中不应写入复杂逻辑代码,保持简洁性和可复用性,大家怎么看缓存

    // store.js
    // 引入必须的库
    const mobx = require('../libs/mobx');
    const extendObservable = require('../libs/mobx').extendObservable;
    const computed = require('../libs/mobx').computed;
    const toJS = require('../libs/mobx').toJS;
    
    let store = function () {
      extendObservable(this, {
    
        // observable data
        players: [],
    
        // computed data
        get count() {
          return this.players.length;
        }
      });
    
      // action
      this.addPlayer = name => {
        let len = this.count;    //此处调用computed data
        let id = len === 0 ? 1 : this.players[len - 1].id + 1;
        this.players.push(new player(name, id));
      }
    }
    
    export default store;
  3. 全局引入store
    众所周知,使用mobxstore要使用new store(),若是咱们想全局调用,势必不可能在每一个页面都new一个sotre,由于这样的话每一个页面的store都是一个全新的store。在这里,我在app.js里引入store,并挂载在全局变量globalData下。另外,小程序中不支持路径的省略。网络

    //app.js
    const observer = require('./libs/observer').observer;
    import store from './stores/index';  // 小程序中不支持省略调用
    
    App(observer({
      onLaunch: function () {
      },
      globalData: {
        store: new store()
      }
    }))
  4. 在pages里调用全局的store
    能够同时使用内置的data进行双向绑定哦app

    // index.js
    const observer = require('../../libs/observer').observer;
    
    let app = getApp();
    Page(observer({
      data: {
        mes: 'hello jim green'
      },
      props: {
        store: app.globalData.store
      },
    }))
  5. 在页面中调用storeide

    <view class="players-list">
      <view class="players-item" wx:for="{{props.store.players}}" wx:key="{{item.id}}">    // 调用observable data
        <text class="players">{{item.id}}:{{item.name}}</text>
      </view>
      <view>{{props.sotre.count}}</view>    //  调用computed data
    </view>
  6. 更新多个页面的store
    问题来了,这个时候,多个页面的store仍是独立的,如何所有更新呢?答案就是在onShowonHide或者onUnload这三个生命周期函数中跟新全局的store

    onShow: function() {    // 显示时更新本页面store
      this.props.store = app.globalData.store
    },
    onHide: function() {   // 隐藏时更新全局store
      app.globalData.store = this.props.store;
    },
    onUnload: function() {    // 页面跳转返回时更新全局store
      app.globalData.store = this.props.store;
    },
  7. store和localStorage的长效存储
    考虑到网络还有程序崩溃的问题,我将store存储在localStorage中以便恢复,我在index.jsonLoad中调用get storage,在onHideset storage。因为toJS方法返回了一个不支持[Symbol.iterator]()的对象,因此在store里进行了以下设置

    // index.js
    onLoad: function () {
      let store = wx.getStorageSync('store');
      if(store) {
        this.props.store.formStorageToStore(store);
      }
    },
    onHide: function () {
      
      let store =this.props.store.currentStore;
      wx.setStorageSync('store', store)
    },
    
    // store.js
      get currentStore() {
        let {players,games,currentGame,hidden,filter} = toJS(this);
        return {players,games,currentGame,hidden,filter};
      }
      this.formStorageToStore = ({players,games,currentGame,hidden,filter}) => {
        this.players = players;
        this.games = games;
        this.currentGame = currentGame;
        this.hidden = hidden;
        this.filter = filter;
      }

others

讲点其余

  • 本项目的示例小程序地址weapp-bmscore,欢迎各位老铁点个关注666

相关文章
相关标签/搜索