完整的教务系统前端项目 - 包含所有修复和9月份数据
This commit is contained in:
30
src/store/index.js
Normal file
30
src/store/index.js
Normal file
@@ -0,0 +1,30 @@
|
||||
// 古老写法
|
||||
// // 1. 导入原始 Redux 的核心函数
|
||||
// import { createStore, combineReducers } from 'redux';
|
||||
// // 2. 导入 reducers(保持不变)
|
||||
// import loadingReducer from "./slices/loadingSlice";
|
||||
// import studentReducer from "./slices/studentSlice";
|
||||
|
||||
// // 3. 合并 reducers
|
||||
// const rootReducer = combineReducers({
|
||||
// loading: loadingReducer,
|
||||
// student: studentReducer,
|
||||
// });
|
||||
|
||||
// // 4. 创建 store(原始 Redux 方式)
|
||||
// export default createStore(
|
||||
// rootReducer,
|
||||
// // 可选:添加 Redux DevTools 支持
|
||||
// window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
|
||||
// );
|
||||
|
||||
import { configureStore } from "@reduxjs/toolkit";
|
||||
import loadingReducer from "./slices/loadingSlice";
|
||||
import studentReducer from "./slices/studentSlice";
|
||||
|
||||
export default configureStore({
|
||||
reducer: {
|
||||
loading: loadingReducer,
|
||||
student: studentReducer,
|
||||
},
|
||||
});
|
||||
31
src/store/slices/loadingSlice.js
Normal file
31
src/store/slices/loadingSlice.js
Normal file
@@ -0,0 +1,31 @@
|
||||
// 导入 Redux Toolkit 的 createSlice
|
||||
import { createSlice } from "@reduxjs/toolkit";
|
||||
|
||||
// 定义初始状态
|
||||
const initialState = {
|
||||
effect: {}, // 全局加载状态对象
|
||||
};
|
||||
|
||||
// 创建 loading slice
|
||||
const loadingSlice = createSlice({
|
||||
name: "loading", // slice 名称
|
||||
initialState,
|
||||
reducers: {
|
||||
// 显示全局加载状态
|
||||
showGlobalLoading: (state, action) => {
|
||||
const { namespace } = action.payload;
|
||||
state.effect[namespace] = true;
|
||||
},
|
||||
// 隐藏全局加载状态
|
||||
hideGlobalLoading: (state, action) => {
|
||||
const { namespace } = action.payload;
|
||||
state.effect[namespace] = false;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// 导出 action creators
|
||||
export const { showGlobalLoading, hideGlobalLoading } = loadingSlice.actions;
|
||||
|
||||
// 导出 reducer
|
||||
export default loadingSlice.reducer;
|
||||
39
src/store/slices/studentSlice.js
Normal file
39
src/store/slices/studentSlice.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import { createSlice } from "@reduxjs/toolkit";
|
||||
|
||||
// 定义初始状态
|
||||
const initialState = {
|
||||
studentInfo: null, // 用户信息初始为null
|
||||
isLoggedIn: false, // 登录状态初始为false
|
||||
};
|
||||
|
||||
const studentSlice = createSlice({
|
||||
name: "student",
|
||||
initialState,
|
||||
reducers: {
|
||||
// 设置用户信息
|
||||
setStudentInfo: (state, action) => {
|
||||
state.studentInfo = action.payload;
|
||||
state.isLoggedIn = true;
|
||||
},
|
||||
// 清除用户信息
|
||||
clearStudentInfo: (state) => {
|
||||
state.studentInfo = null;
|
||||
state.isLoggedIn = false;
|
||||
},
|
||||
// 更新用户信息的部分字段
|
||||
updateStudentInfo: (state, action) => {
|
||||
if (state.studentInfo) {
|
||||
state.studentInfo = {
|
||||
...state.studentInfo,
|
||||
...action.payload,
|
||||
};
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// 导出actions
|
||||
export const { setStudentInfo, clearStudentInfo, updateStudentInfo } =
|
||||
studentSlice.actions;
|
||||
|
||||
export default studentSlice.reducer;
|
||||
Reference in New Issue
Block a user