Skip to content

Commit

Permalink
actions 配合 mapActions 并传参
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnywwy committed Apr 19, 2022
1 parent 0bb4926 commit 5fe1bd4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/components/Subtraction.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
<button @click="btnSub">-1</button>
|
<button @click="btnSubN">-N</button>
|
<button @click="btnSubAsync">异步1秒 -1</button>
|
<button @click="btnSubAsyncN">-N</button>
</div>
</template>

<script>
import {mapState, mapMutations} from 'vuex'
import {mapState, mapMutations, mapActions} from 'vuex'
export default {
data() {
Expand All @@ -20,13 +24,19 @@ export default {
},
methods: {
...mapMutations(['sub', 'subN']),
...mapActions(['subAsync', 'subAsyncN']),
btnSub() {
this.sub()
},
btnSubN() {
this.subN(3)
},
btnSubAsync() {
this.subAsync()
},
btnSubAsyncN() {
this.subAsyncN(3)
}
},
Expand Down
12 changes: 11 additions & 1 deletion src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,17 @@ export default new Vuex.Store({
setTimeout(() => {
context.commit('addN',payload)
},1000)
}
},
subAsync(content) {
setTimeout(() => {
content.commit('sub')
},1000)
},
subAsyncN(content,payload) {
setTimeout(() => {
content.commit('subN',payload)
},1000)
},
},
modules: {}
})

0 comments on commit 5fe1bd4

Please sign in to comment.