zoukankan      html  css  js  c++  java
  • vuex 让 store 通天

    为了让 子组件 不用时刻引用 store文件

    store.js

    import Vue from "vue";
    import Vuex from "vuex";
    
    Vue.use(Vuex); //必须用Vue.use()来安装
    const store = new Vuex.Store({
        //这里的state必须是JSON,是一个对象。
        state: {
            count: 0         //这里就是初始值的罗列
        },
        //突变,罗列所有可能改变state的方法
        mutations: {
            //没有所谓的大写字母的Type了,就是一个一个函数
            add (state) {
                  state.count++;
            },
            minus (state) {
                state.count--;
            }
        }
    });
    
    export default store;

    main.js:

    import Vue from "vue";
    import Vuex from "vuex";
    import MyCompo from "./MyCompo.vue";
    
    
    import store from "./store.js";
    
    new Vue({
        el : "#app",
        store,
        data : {
            a : 100
        },
        render: h => h(App)
    });

    .vue组件文件中,使用this.$store来表示使用的那个全局store

    <style scopoed>
     
    </style>
    
    <template>
        <div>
            我是子组件
            <h1>
                <button v-on:click="minusnandler">减少</button>
                {{count}}
                <button v-on:click="addhandler">增加</button>
            </h1>
        </div>
    </template>
    
    <script>
        export default {
            computed : {
                count(){
                    return this.$store.state.count;
                }
            },
            methods : {
                addhandler(){
                    this.$store.commit("add");
                },
                minusnandler(){
                    this.$store.commit("minus");
                }
            }
        }
    </script>
  • 相关阅读:
    补番完了 来自深渊
    160CrackMe第十九Brad Soblesky.2
    MyBio小隐本记注册破解
    WDTP注册破解
    对话框和普通窗口工作方式的区别
    Win32汇编学习(11):对话框(2)
    Win32汇编学习(10):对话框(1)
    MongoDB的复制源oplog
    Windows搭建MongoDB复制集
    MangoDB的下载和安装
  • 原文地址:https://www.cnblogs.com/caoleyun/p/12693046.html
Copyright © 2011-2022 走看看