feat: 🎸 新增了redux用于全局自动loading

This commit is contained in:
2025-08-20 23:56:04 +08:00
parent 9895101fde
commit a493f2773c
11 changed files with 477 additions and 150 deletions

View File

@@ -1,25 +1,31 @@
// 导入 Redux Toolkit 的 createSlice
import { createSlice } from "@reduxjs/toolkit";
// 创建slice
// 定义初始状态
const initialState = {
effect: {}, // 全局加载状态对象
};
// 创建 loading slice
const loadingSlice = createSlice({
name: "loading",
initialState: {
value: false,
},
name: "loading", // slice 名称
initialState,
reducers: {
// 设置loading状态为true
setLoadingTrue: (state) => {
state.value = true;
// 显示全局加载状态
showGlobalLoading: (state, action) => {
const { namespace } = action.payload;
state.effect[namespace] = true;
},
// 设置loading状态为false
setLoadingFalse: (state) => {
state.value = false;
// 隐藏全局加载状态
hideGlobalLoading: (state, action) => {
const { namespace } = action.payload;
state.effect[namespace] = false;
},
},
});
// 导出actions
export const { setLoadingTrue, setLoadingFalse } = loadingSlice.actions;
// 导出 action creators
export const { showGlobalLoading, hideGlobalLoading } = loadingSlice.actions;
// 导出reducer
// 导出 reducer
export default loadingSlice.reducer;