vue中的vuex的使用

对于vuex的使用,之前一直很蒙蔽,看了几次官网的介绍了解其表意,但是在具体使用的时候还是很懵逼,故而结合实际项目,实战了一番,最终明了;

vuex除了管理公用数据之外,还解决了非父子组件之间的数据传递;

使用方法:

1.安装vuex;  

npm i vuex --save-dev

2.创建个单独的store文件夹,存放相关文件

3.在store文件夹下创建index.js文件,文件中的内容为:

import vue from 'Vue'

import Vuex from 'Vuex

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        appUserInfo: {},
    },
    mutations: {
        setAppUserInfo(state, obj) {
            state.appUserInfo = obj.data;
        },
       
    }
});

export default store;
4.在main.js文件中注册store;
import stroe from '@/store/index.js'
new Vue({store})
5.在组件中使用
 this.$store.commit({
                    type: "setAppUserInfo",
                    data: {
                    login_name: "houqin4" //01000000 赵志东  01010000 郑立志
                }
            }); 

猜你喜欢

转载自www.cnblogs.com/mamifeng/p/11929528.html