vuex mapActions

在組件中使用 this.$store.dispatch('xxx') 分發 action,或者使用 mapActions 輔助函數將組件的 methods 映射爲 store.dispatch 調用(須要先在根節點注入 store).css

import Vue from 'vue'; import Element from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' import App from './App'; import router from './router'; import Vuex from 'vuex'; Vue.config.productionTip = false; Vue.use(Vuex); Vue.use(Element) //vuex的配置 //注意Store是大寫
const store = new Vuex.Store({ //數據保存
 state: { show: false, count: 0, list: [1, 5, 8, 10, 30, 50] }, mutations: { increase(state, n = 1) { state.count += n; }, decrease(state, n = 1) { state.count -= n; }, switch_dialog(state) { // 這裏的state對應着上面這個state
            state.show = state.show ? false : true
            // 你還能夠在這裏執行其餘的操做改變state
 } }, getters: { filteredList: state => { return state.list.filter(item => item < 31); } }, actions: { asyncDecrease({commit }) { commit('decrease',5); }, switch_dialog123(context) { // 這裏的context和咱們使用的$store擁有相同的對象和方法
            context.commit('switch_dialog') // 你還能夠在這裏觸發其餘的mutations方法
 } } }); /* eslint-disable no-new */
new Vue({ el: '#app', router, //使用vuex
 store: store, render: h => h(App), });
<template>
    <div> {{count}} <button @click="handleIncrease">+5</button>
        <button @click="handleDecrease">-5</button>
        <button @click="handleAsyncDecrease">異步-5</button>
        <button @click="handleRouter">跳轉到 HelloWorld3</button>
        <button @click="showRouter">展現路由</button>
    </div>
</template>

<script> import { mapState } from 'vuex' import { mapGetters } from 'vuex' import { mapMutations } from 'vuex' import { mapActions } from 'vuex' export default { name: 'HelloWorld2', computed: { // count(){
            // return this.$store.state.count;
            // },
            // filteredList() {
            // return this.$store.getters.filteredList;
            // },
 ...mapState({ count: state => state.count }), // 使用對象展開運算符將 getter 混入 computed 對象中
 ...mapGetters([ 'filteredList' ]) }, methods: { handleIncrease() { // this.$store.commit('increase', 5);
                this.increase(); }, handleDecrease() { this.$store.commit('decrease', 5); }, handleAsyncDecrease() { //調用方式一 // this.$store.dispatch('asyncDecrease'); //調用方式二 this.asyncDecrease() }, handleRouter() { this.$router.push('/HelloWorld3'); }, showRouter() { console.log(this.$router); console.log(this.$router.push); }, //mapMutations 使用方法一
            // ...mapMutations([
            // 'increase', // 將 `this.increase()` 映射爲 `this.$store.commit('increase')`
            // ]),
            //mapMutations 使用方法二
 ...mapMutations({ increase: 'increase' // 將 `this.increase()` 映射爲 `this.$store.commit('increase')`
 }), //mapActions 使用方法一 // ...mapActions([ // 'asyncDecrease' // 將 `this.asyncDecrease()` 映射爲 `this.$store.dispatch('asyncDecrease')` // ]), //mapActions 使用方法二  ...mapActions({ asyncDecrease: 'asyncDecrease' // 將 `this.asyncDecrease()` 映射爲 `this.$store.dispatch('asyncDecrease')`  }), } }; </script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
相關文章
相關標籤/搜索