vuex状态 显示和隐藏封装

本文介绍了如何在Vue应用中使用Vuex进行状态管理,重点讲解了如何通过`dispatch`和`commit`操作来显示和隐藏全局加载效果。通过实例展示了`closeGlobalLoading`和`openGlobalLoading`方法,并在main.js中进行了全局注入和页面方法实现。
摘要由CSDN通过智能技术生成

状态管理显示和隐藏
在这里插入图片描述

在这里插入图片描述

const commonFn={
    // 显示和隐藏
    // dispatch:含有异步操作,例如向后台提交数据,写法: this.$store.dispatch('action方法名',值)
    // commit:同步操作,写法:this.$store.commit('mutations方法名',值)
    closeClobalLoading(){
        setTimeout(()=>{
            store.dispatch('showLoading',false)
        },0)
    },
    openGlobalLoading(){
        setTimeout(()=>{
            store.dispatch('showLoading',true)
        },0)
    },

vuex状态管理
在这里插入图片描述

// main.js引入store全局注入
import Vue from 'vue';
import Vuex from 'vuex';
import collection from './modules/collection'
import footerStatus from './modules/footerStatus'
Vue.use(Vuex);
const store = new Vuex.Store({
    modules:{
        collection,
        footerStatus
    },
    state:{ //要设置的全局访问的state对象
        // 设置初始属性值
        count:1,
        showLoading:false
    },
    getters:{ //实时监听state值的变化(最新状态)
        // 方法名随意
        getcount:state=>{
            return state.count+1
        }
    },
    mutations:{//唯一允许更改state
        showLoading(state, status) {
            state.showLoading = status
          },
        add(state,n){
            state.count=state.count+n
        },
        remove(state){
            state.count=state.count-1
        }
    },
    actions:{//触发更改
        showLoading ({ commit }, status) {
            commit('showLoading', status)
          },
        addFun(context,n){
            context.commit("add",n)
        },
        removeFun(context){
            context.commit("remove")
        }

    }
});
 
export default store;

页面显示
在这里插入图片描述

  methods: {
            // 点击显示
            showsure(){
                _g.openGlobalLoading()
            },
            // 点击影藏
            showfalse(){
            _g.closeClobalLoading()
            },
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值