This is a connector to make a vue component has its own redux store.
demo: https://github.com/RequireSun/vue-single
- import the
wrapper
function and use it to wrap the object<script>
tag exported.
import wrapper from 'vue-own-redux/wrapper';
export default wrapper({
name: 'xxx',
...
});
-
create a store and its actions with
redux
andredux-actions
.use
bindActions
to bind actions on the store.counter.js
import {createStore} from 'redux';
import {createActions,handleActions} from 'redux-actions';
import bindActions from 'vue-own-redux/bindActions';
const ADD = 'ADD';
const MINUS = 'MINUS';
export default function () {
const reducers = handleActions({
[ADD](state, action) {
return {
...state,
count: state.count + action.payload.amount,
}
},
[MINUS](state, action) {
return {
...state,
count: state.count - action.payload.amount,
}
}
}, {
count: 0,
});
const store = createStore(reducers);
const actions = bindActions(store, createActions({
[ADD]: info => info,
[MINUS]: info => info,
}));
return {
actions,
store,
};
}
-
import your component where you want, create new
store
&actions
, then pass them into your component.the
business
property must have two propertiesstore
andactions
.businesses
property also can be an object that contains many businesses, when you pass a businesses object in this format, thestate
property in Vue component will also provide states in this format.page.vue
<template>
<div>
<h1>{{ msg }}</h1>
<counter :business="stores"></counter>
<counter :business="stores2"></counter>
</div>
</template>
<script>
import counter from '../counter';
import elCounter from '../counter.vue';
export default {
name: 'HelloWorld',
components: {
counter: elCounter,
},
data () {
return {
msg: 'Welcome to Your Vue.js App',
stores: {},
stores2: {},
}
},
mounted () {
this.stores = counter();
this.stores2 = counter();
}
}
</script>
-
import the component of
vue-own-redux
as a component of your vue component.define the way how parent element pass the store and actions in:
- provide
stores
in the props property. - pass the
stores
into connect component. - use the
state
andaction
in the children components of connect by theslot-scope
feature.
- provide
<template>
<div>
<connect :stores="stores">
<div slot-scope="scope">
<h1>{{scope.state}}</h1>
<button @click="add">actions in methods</button>
<button @click={scope.action.minus({amount:1})}>actions on element</button>
</div>
</connect>
</div>
</template>
<script>
import connect from 'vue-own-redux';
export default {
name: 'my-component',
props: ['stores'],
components: {connect},
};
</script>
-
create a store and its actions with
redux
andredux-actions
.use
bindActions
to bind actions on the store.counter.js
import {createStore} from 'redux';
import {createActions,handleActions} from 'redux-actions';
import bindActions from 'vue-own-redux/bindActions';
const ADD = 'ADD';
const MINUS = 'MINUS';
export default function () {
const reducers = handleActions({
[ADD](state, action) {
return {
...state,
count: state.count + action.payload.amount,
}
},
[MINUS](state, action) {
return {
...state,
count: state.count - action.payload.amount,
}
}
}, {
count: 0,
});
const store = createStore(reducers);
const actions = bindActions(store, createActions({
[ADD]: info => info,
[MINUS]: info => info,
}));
return {
actions,
store,
};
}
-
import your component where you want, create new
store
&actions
, then pass them into your component.the
stores
property must have two propertiesstore
andactions
.page.vue
<template>
<div>
<h1>{{ msg }}</h1>
<counter :stores="stores"></counter>
<counter :stores="stores2"></counter>
</div>
</template>
<script>
import counter from '../counter';
import elCounter from '../counter.vue';
export default {
name: 'HelloWorld',
components: {
counter: elCounter,
},
data () {
return {
msg: 'Welcome to Your Vue.js App',
stores: {},
stores2: {},
}
},
mounted () {
this.stores = counter();
this.stores2 = counter();
}
}
</script>