Coder Social home page Coder Social logo

wex's Introduction

使用方法

初始化&引入

新建一个文件为store.js 内容为

/**
 * no use strict 
 */
wx.wex = new Wex().init({
  state: {
    obj: '咩咩咩咩',
    TS: 2,
    peopleNum: 0,
    dishesTypeId: 1,
    dishesTypeName: '',
    test:{
      a:1,
      b:{a:2}
    }
  },
  Mutation: {
    getFoodInfo: () => {

    },
    setTest:({setState,args})=>{
      setState('test',args[0])
    },
    getTest:({state})=>{
      return state.test;
    },
    getdishesTypeId: ({state}) => {
      return state.dishesTypeId
    },
    setdishesTypeId: ({setState,args}) => {
      setState('dishesTypeId', args[0] );
    },
    setdishesTypeName: ({setState,args}) => {
      setState('dishesTypeName', args[0] );
    },
    getdishesTypeName: ({state}) => {
      return state.dishesTypeName;
    },
    getObj: ({state}) => {
      return state.obj
    },
    tt: ({state}) => {
      return state.TS
    },
    setTs: ({setState,args}) => {
      setState('TS', args[0])
      
    },
    setObj: ({setState,commit,args}) => {
      setState('obj', args[0])
      commit('setTs',args[0])
    },
    setPeopleNum: ({setState,args,state}) => {
      state.peopleNum = args[0]
      // setState('peopleNum', args[0] )
    },
    getPeopleNum: ({state}) => {
      return state.peopleNum
    }

  }
});

在入口文件中引入

import './store'

在需要使用的地方

// 如果只是需要使用获取值,设置值
    var {
        setPeopleNum,
        getTest,
        setTest
    } = wx.wex.mapMutations;
setPeopleNum(10)
    var {
        peopleNum
    } = wx.wex.state
// 如果需要是值具有响应功能
wx.wex.on('dishesTypeId', (oj) => {
     this.dishesTypeId = oj;
}, this)
// 使用箭头函数保证作用域指向,在回调填写响应后的变化
// 同一页面内多次订阅同一状态会只记录最后一次订阅,防止多次无意义订阅造成内存泄漏
// 无法直接更改state和mutation,使用wx.wex.history可以查询到全部的变更记录
// 新增部分数组变异方法,在Mutation里直接操作state也可以观测到变化,不过setState在某些情况下性能更好一些。

wex对象提供state(取值),commit(提交方法),on(订阅),emit(触发),off(删除),mapMutations(映射mutations方法),history(变更记录)几个方法

在mutaions里参数包含state,setState,commit,args4个参数。

wex's People

Contributors

mayako21126 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.