VUEX使用记录

1,安装:npm install vuex --save;

2,main.js同级目录建文件荚store 在文件荚里建index.js,

内容如下:

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);
export default new Vuex.Store({
    state:{},
    mutations:
    {
        set(state,o)
        {
          state[o.k]=o.v;
        }
    },
})

3,main.js中引入:

import store from './store'
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>',
})

4,在组件中使用:

methods:{
        qd(){
            this.$emit('qd','1')
        },
        cli(){
           var o={
             k:"name",
             v:"gaogaogaogao",
           }
           this.$store.commit('set',o)
           console.log(this.$store.state.name);
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_22936647/article/details/81301396