VueVue

Vuex快速上手

2020-02-21  本文已影响0人  learninginto

Vuex快速上手

通俗的解释:是一种最好的非父子组件传值的一种方案。

官方解释:Vuex是一个公共状态管理(公共的内存对象),它把所有组件需要共有的状态放到了一个公共的内存空间里。并且给每一个状态做了一个数据劫持(给每一个状态添加了一个getter和setter方法);

一、Vuex安装使用

cnpm install -g @vue/cli
vue create 项目名称

有default默认和Manually手动配置,通常选择Manually。(通过上下键进行选择)

空格选中,回车跳过。可以先选择必要的配置,其他需要的时候再安装。

vue-cli.png

Use history mode for router?(是否使用history路由)

cnpm install Vuex -S
import Vue from "vue"
import Vuex from "vuex";
Vue.use(Vuex);
const store = new Vuex().store({

})
export default stroe;
new Vue({
    store,
    render:h => h(app)
}).$mount('#app')

二、Vuex常用的配置项

总结:mapMutions和mapActions是映射到methods上,mapState和mapGetter是映射到computed上

三、数据传递的流程

store中的数据通过辅助函数或this.$store.state.属性的方式渲染到组件上,组件需要通过this.$store.dispatch来调用actions中的函数,actions函数通过commit方法调用mutations方法,修改state中的数据。因为数据是响应式的,因此组件视图也随之改变。

Vuex.png

注意:基于这种数据传递的方式,所以,不能在组件内部使用v-model等来修改store中的数据。否则会导致错误难以理解。

通过提交 mutation 的方式,而非直接改变 store.state.count,是因为我们想要更明确地追踪到状态的变化。

one和two两个组件共用state的数据:在one组件中修改state,会调用one组件中的dispatch,进入store中的actions,通过commit接收,传送到store中的mutation中修改state的值,重新渲染页面,one和two组件的值被更新

<template>
    <div>
        <h2>这是一个One组件</h2>
        <p>{{$store.state.n}}</p>
        <button @click="handleAdd">点击</button>
    </div>
</template>

<script>
export default {
    data(){
        return {
            message:"lxc"
        }
    },
    created() {
        console.log(this);
    },
   methods:{
        handleAdd(){
           this.$store.dispatch("handleActionsAdd","lxc")
        }
   }
}
</script>
<template>
    <div>
         <h2>这是一个Two组件</h2>
         <p>{{$store.state.n}}</p>
    </div>
</template>
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);

const store = new Vuex.Store({
    state: {
        n: 10
    },
    actions: {
        handleActionsAdd({ commit }, params) {
            commit("handleMutationsAdd", params)
        }
    },
    mutations: {
        handleMutationsAdd(state) {
            state.n++;
        }
    }
})

export default store;

四、Vuex的简易封装

let Vue;
function install(_Vue) {
    //vue.use是找到install方法,给他传递一个vue
    Vue = _Vue;
    Vue.mixin({
        //将store的实例挂在vue的实例身上   this.$store.state.属性
        beforeCreate() {

            if (this.$options.store) {
                Vue.prototype.$store = this.$options.store;
            }
        },
    })
}

class Store {
    //初始化
    constructor(options) {
        //将state中的状态转换为响应式状态
        this.state = new Vue({
            data: options.state
        })

        //初始化mutations
        this.mutations = options.mutations || {};

        //初始化actions
        this.actions = options.actions || {};
        
        //初始化getters
        options.getters && this.handleGetters(options.getters)
    }
    
    commit(eventName, params) {
        var fn = this.mutations[eventName];
        fn(this.state, params);
    }
    
    dispatch(eventName, params) {
        var fn = this.actions[eventName];
        fn({ commit: this.commit.bind(this), dispatch: this.dispatch.bind(this), state: this.state }, params);
    }
    
    handleGetters(getters) {
        var that = this;
        this.getters = {};

        Object.keys(getters).forEach(key => {
            Object.defineProperty(that.getters, key, {
                get: function () {
                    return getters[key](that.state);
                }
            })
        })
    }
}
上一篇 下一篇

猜你喜欢

热点阅读