BubbleSci 428303a1fd add English and Chinese translation | 4 years ago | |
---|---|---|
.. | ||
dist | 4 years ago | |
CHANGELOG.md | 4 years ago | |
README.md | 4 years ago | |
README_EN.md | 4 years ago | |
helper.js | 5 years ago | |
index.js | 5 years ago | |
install.js | 5 years ago | |
package.json | 4 years ago |
English | 简体中文
npm install @wepy/redux redux --save
// app.wpy
import wepy from '@wepy/core';
import wepyRedux from '@wepy/redux';
wepy.use(wepyRedux);
// ~/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;
}
}));
// ~/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' })
}
}
})
状态:字符串/数组/K-V对象.。需要映射的 state 属性。如:
mapState('counter')
mapState(['counter', 'somethingelse'])
mapState({ alias: 'counter' })
mapState({
num: function (state) {
return state.counter.num;
}
})
actions: K-V Object. 需要映射的 action 。如:
mapActions({ add: 'ADD' });
mapActions({
add: function () {
return {
type: 'ADD'
};
}
});