微信小程序状态管理工具 JStore

微信小程序状态管理工具 JStore

闲着没事做,就想着给微信小程序写一个状态管理工具,名叫 JStore,这个状态管理工具是仿照 vuex 的几个方法来写的,所以有 vuex 的基础同学很容易理解。

Github:https://github.com/GeorgeLeoo/JStore

  1. 安装
    在小程序根目录下新建一个 store 文件,将 jstore.js 引入小程序项目。
  1. 准备
    在 store 中新建index.js, actions.js, mutations.js, mutation-types.js, getters.js, state.js

state.js:是一个状态管理对象,用来管理所有的状态, state中所有属性不能被直接删除或者修改,必须通过 dispatch 或 getter 方法

module.exports = {
    userInfo: {},
    arr:[2,4,6,8]
};

mutation-types.js:包含多个 mutation 的 type 名称的常量

export const SAVE_USER_INFO =  'save_user_info';
export const UPDATE_USER_INFO =  'UPDATE_USER_INFO';
export const SAVE_WEATHER =  'save_weather';

mutations.js: 直接更新 state 的多个方法的对象

const {SAVE_USER_INFO,SAVE_WEATHER} = require('./mutation-types');

module.exports = {
    [SAVE_USER_INFO](state, {userInfo}) {
        state.userInfo = userInfo;
    },
    [SAVE_WEATHER](state, {weatherinfo}) {
        state.weatherinfo = weatherinfo;
    }
};

actions.js: 通过 mutation 间接更新 state 对象

const {SAVE_USER_INFO, SAVE_WEATHER} = require('./mutation-types');

module.exports = {
    saveUserInfo({commit, state}, {userInfo}) {
        console.log(userInfo);
        commit(SAVE_USER_INFO, {userInfo})
    },
    saveWeather({commit, state}) {
        wx.request({
            url: 'http://www.weather.com.cn/data/sk/101010100.html',
            success(res) {
                console.log(res.data);
                let {weatherinfo} = res.data;
                commit(SAVE_WEATHER, {weatherinfo})
            }
        })
    }
};

getter.js:store 的计算属性, 只有当它的值发生了改变才会被重新计算。

module.exports = {
    addTen: state => {
        return state.arr = state.arr.map((val) => val * 10);
    }
};

index.js: vuex最核心的管理对象

const Jstore = require('./jstore');
const state = require('./state');
const mutations = require('./mutations');
const actions = require('./actions');
const getters = require('./getters');

module.exports = new Jstore({
    state,
    mutations,
    actions,
    getters
});

在 app.js 中引入 index.js

const store = require('./store/index.js');

然后在apponLaunch()方法中调用store(this)store 注册到小程序中,以后再 Page 中直接使用 getApp().$store 获取 store 对象,其中 this 指向的是 App 对象。

假设将获取微信账户数据添加到 state 中,可以在获取数据后,使用以下方法将

getApp().$store.dispatch("saveUserInfo", {userInfo: e.detail.userInfo});

结果就将userInfo数据保存到 state 中,然后可以在页面的任意地方调用这个数据


image.png

dispatch有两个参数,第一个参数是 action 中定义的函数名,第二个参数是要保存的数据要传哪些参数给 action 中的方法,

比如获取天气数据:
在点击获取数据按钮时,执行以下这条语句

getApp().$store.dispatch("saveWeather");

结果:

image.png

当state 中有数据后,取出数据使用 getState 方法

getApp().$store.getState(["userInfo", "weatherinfo"], this);

getState()方法有两个参数,第一个参数是一个数组,数组中存放的都是state 中的 key 的名称,而二个参数是当前页面的 this 指向,如果设置了 this,就会将数据保存到当前页面的 data 中

image.png

微信小程序状态管理工具 JStore api

API参考

JStore

const store = new JStore({...options})

JStore构造器选项

state

  • 类型:object
    在 store 上注册 state, state 是一个状态管理对象,用来管理所有的状态

mutations

  • 类型:{ [type: string]: Function }
    在 store 上注册 mutation,处理函数总是接受state作为第一个参数, payload作为第二个参数(可选)

actions

  • 类型:{ [type: string]: Function }
    在 store 上注册 action。处理函数总是接受context作为第一个参数,payload作为第二个参数(可选)。
    context对象包含以下属性:
{
  state,      // 等同于 `store.state`
  commit,     // 等同于 `store.commit`
}

getters

  • 类型:{ [type: string]: Function }
    在 store 上注册 getter,getter 方法接受 state 参数

JStore 实例属性

state

  • 类型:Object
    根状态,只读,state 内部的第一层数据也是只读的

JStore 实例方法

commit

  • commit(type: string, payload?: any)
    提交 mutation。

dispatch

  • commit(type: string, payload?: any)
    分发 action。

getState

  • commit(type: array, context?: object)
    获取 state。
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Vuex是什么? Vuex 是一个专为 Vue.js应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件...
    萧玄辞阅读 3,223评论 0 6
  • ### store 1. Vue 组件中获得 Vuex 状态 ```js //方式一 全局引入单例类 // 创建一...
    芸豆_6a86阅读 437评论 0 0
  • Vuex 是什么? Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有...
    skycolor阅读 926评论 0 1
  • 安装 npm npm install vuex --save 在一个模块化的打包系统中,您必须显式地通过Vue.u...
    萧玄辞阅读 3,032评论 0 7
  • 今天有打电话让报班考消防工程师证的,大家有没有好的推荐啊?,有人了解没有啊
    穹海源梦阅读 45评论 0 0

友情链接更多精彩内容