BubbleSci 428303a1fd add English and Chinese translation 4 years ago
..
dist 1a3b4e5c02 build: build dist 4 years ago
CHANGELOG.md d995f52a61 chore(release): publish v2.1.0 4 years ago
README.md 428303a1fd add English and Chinese translation 4 years ago
README_EN.md 428303a1fd add English and Chinese translation 4 years ago
helper.js 0ff522c870 style: eslint style fixs part II 5 years ago
index.js 02aa812725 style: eslint auto fix 5 years ago
install.js 0ff522c870 style: eslint style fixs part II 5 years ago
package.json d995f52a61 chore(release): publish v2.1.0 4 years ago

README.md

English | 简体中文

WePY 2.0中的Redux

安装

npm install @wepy/redux redux --save

用法

  1. 安装Redux
// app.wpy
import wepy from '@wepy/core';
import wepyRedux from '@wepy/redux';

wepy.use(wepyRedux);
  1. 初始化 Store
// ~/store.js
import { createStore, combineReducers } from 'redux';

export default createStore(combineReducers({
  counter (state = { num: 0 }, action) {
    switch (action.type) {
      case 'ADD':
        return {
          ...state,
          num: state.num + 1
        };
    }
    return state;
  }
}));
  1. 映射到组件
// ~/counter.wpy
<template>
  <div> {{counter.num}} </div>
  <button @tap="increment"> Increment </button>
</template>
<script>
import wepy from '@wepy/core';
import { mapState } from '@wepy/redux';
import store from './store'

wepy.component({
  store,
  computed: {
    ...mapState([ 'counter' ])
  },
  methods: {
    increment () {
      this.$store.dispatch({ type: 'ADD' })
    }
  }
})

API

  • mapState(states)

状态:字符串/数组/K-V对象.。需要映射的 state 属性。如:

  mapState('counter')
  mapState(['counter', 'somethingelse'])
  mapState({ alias: 'counter' })
  mapState({ 
    num: function (state) {
      return state.counter.num;
    } 
  })
  • mapActions(actions)

actions: K-V Object. 需要映射的 action 。如:

  mapActions({ add: 'ADD' });
  mapActions({ 
    add: function () {
      return {
        type: 'ADD'
      };
    } 
  });

Document

https://redux.jg.org