Vuex简单梳理+使用步骤

Vuex简单梳理+使用步骤

一、基础梳理

使用情景(中大型单页面应用)
  • 多个视图依赖同一个状态
  • 来自不同视图的行为需要变更同一个状态
1、状态:State——需要公用的数据

调用方法:

this.$store.state.name;

<script>
import { mapState } from 'vuex'; // 从vuex中导入mapState
export default {
    mounted() {
        console.log(this.name);
    },
    computed: {
        ...mapState(['name']), // 经过解构后,自动就添加到了计算属性中,此时就可以直接像访问计算属性一样访问它
    },
}
</script>
2、修饰符:Getter——不需要修改state时使用,只修饰,不修改

调用方法:

this.$store.getters.getMessage;

//使用mapGetters去解构到计算属性中,就像使用mapState一样,就可以直接使用this调用

<script>
import { mapState, mapGetters } from 'vuex';
export default {
    mounted() {
        console.log(this.name);
        console.log(this.getMessage);
    },
    computed: {
        ...mapState(['name']),
        ...mapGetters(['getMessage']),
    },
}
</script>
3、异步操作:Actions——异步操作时使用

调用方法:

this.$store.dispatch('setNum');

<script>
import { mapActions } from 'vuex';
export default {
    methods: {
        ...mapActions(['setNum']),   // 就像这样,解构到methods中
    },
    async mounted() {
        await this.setNum({ number: 123 });  // 直接这样调用即可
    },
}
</script>

4、修改值:Mutation——修改State的值

调用方法:

this.$store.commit('setNumber');

注意,mapMutations是解构到methods里面的,而不是计算属性了
<script>
import { mapMutations } from 'vuex';
export default {
    mounted() {
        this.setNumberIsWhat({ number: 999 });
    },
    methods: {   // 注意,mapMutations是解构到methods里面的,而不是计算属性了
        ...mapMutations(['setNumberIsWhat']),
    },
}
</script>

二、使用步骤

1、安装
npm install vuex --save
2、src中创建store文件夹,创建index.js文件

完整代码:

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const store = new Vuex.Store({
    state: {
        // 定义一个name,以供全局使用
        name: '张三',
        // 定义一个number,以供全局使用
        number: 0,
        // 定义一个list,以供全局使用
        list: [
            { id: 1, name: '111' },
            { id: 2, name: '222' },
            { id: 3, name: '333' },
        ]
    },
    // 在store对象中增加getters属性
    getters: {
        getMessage(state) { // 获取修饰后的name,第一个参数state为必要参数,必须写在形参上
            return `hello${state.name}`;
        }
    },
	mutations: { // 增加nutations属性
        setNumber(state) {  // 增加一个mutations的方法,方法的作用是让num从0变成5,state是必须默认参数
            state.number = 5;
        }
    },
	actions: {   // 增加actions属性
        setNum(content) {  // 增加setNum方法,默认第一个参数是content,其值是复制的一份store
            return new Promise(resolve => {  // 我们模拟一个异步操作,1秒后修改number为888
                setTimeout(() => {
                    content.commit('setNumberIsWhat', { number: 888 });
                    resolve();
                }, 1000);
            });
        }
    }

});

export default store;

3、在main.js中引用
import Vue from 'vue';
import App from './App';
import router from './router';
import store from './store'; // 引入我们前面导出的store对象

Vue.config.productionTip = false;

new Vue({
    el: '#app',
    router,
    store, // 把store对象添加到vue实例上
    components: { App },
    template: '<App/>'
});

4、组件中使用(见(一、基础梳理)中的调用方法)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值