美文网首页
VUEX最简组件间通信

VUEX最简组件间通信

作者: ozil_oo | 来源:发表于2018-12-22 09:21 被阅读0次

    最简组件间通信

    // CounterBtn.vue
    <template>
        <div>
            <button @click="add">增加</button>
        </div>
    </template>
    
    <script>
    export default {
        methods:{
            add:function() {
                this.$store.commit("increment")
            }
        }
    }
    </script>
    
    // CounterDis.vue
    <template>
        <div>
            <h1>{{count}}</h1>
        </div>
    </template>
    
    <script>
    export default {
      computed: {
        count() {
          return this.$store.state.count;
        }
      }
    };
    </script>
    
    // App.vue
    <template>
        <div>
            <counter-btn></counter-btn>
            <counter-dis></counter-dis>
        </div>
    </template>
    
    <script>
    import CounterBtn from '../components/CounterBtn'
    import CounterDis from '../components/CounterDis'
    export default {
        components:{
            'counter-btn':CounterBtn,
            'counter-dis':CounterDis
        }
    };
    </script>
    
    // store.js
    import Vue from 'vue'
    import Vuex from 'vuex'
    
    Vue.use(Vuex)
    
    export default new Vuex.Store({
      state: {
        count: 0
      },
      mutations: {
        increment (state) {
          state.count++
        }
      },
      actions: {
    
      }
    })
    

    相关文章

      网友评论

          本文标题:VUEX最简组件间通信

          本文链接:https://www.haomeiwen.com/subject/mewukqtx.html