vuex使用

步骤

dispatch --actions
commit -- mutasions

  1. store 目录下下建立一个总
import routerStore from './routerStore'
export default {
    modules: {
        routerStore
    }
};

2.main.js 下引入, 并且创建实例

import Vuex from "vuex";
import storeConfig from './store/index'
Vue.use(Vuex)
const store = new Vuex.Store(storeConfig);
new Vue({
  router,
  render: h => h(App),
  store
}).$mount('#app')

3.创建分支vuex

export default {
    namespaced: true,
    state: {
        col: 'home',

    },
    mutations: {
        changeCol(state, { col }) {
            console.log(col)
            state.col = col;
        }
    },
    actions: {

    }

}

4.组件中使用

computed: {
    col(){
      return this.$store.state.routerStore.col
    }
  },

你可能感兴趣的:(vuex使用)