feat: 🎸 新增了一个全局加载组件
This commit is contained in:
10
src/store/index.js
Normal file
10
src/store/index.js
Normal file
@@ -0,0 +1,10 @@
|
||||
import { configureStore } from '@reduxjs/toolkit';
|
||||
import loadingReducer from './slices/loadingSlice';
|
||||
import userReducer from './slices/userSlice'; // 导入新的用户reducer
|
||||
|
||||
export default configureStore({
|
||||
reducer: {
|
||||
loading: loadingReducer,
|
||||
user: userReducer // 添加用户reducer到store
|
||||
}
|
||||
});
|
||||
25
src/store/slices/loadingSlice.js
Normal file
25
src/store/slices/loadingSlice.js
Normal file
@@ -0,0 +1,25 @@
|
||||
import { createSlice } from "@reduxjs/toolkit";
|
||||
|
||||
// 创建slice
|
||||
const loadingSlice = createSlice({
|
||||
name: "loading",
|
||||
initialState: {
|
||||
value: false,
|
||||
},
|
||||
reducers: {
|
||||
// 设置loading状态为true
|
||||
setLoadingTrue: (state) => {
|
||||
state.value = true;
|
||||
},
|
||||
// 设置loading状态为false
|
||||
setLoadingFalse: (state) => {
|
||||
state.value = false;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// 导出actions
|
||||
export const { setLoadingTrue, setLoadingFalse } = loadingSlice.actions;
|
||||
|
||||
// 导出reducer
|
||||
export default loadingSlice.reducer;
|
||||
38
src/store/slices/userSlice.js
Normal file
38
src/store/slices/userSlice.js
Normal file
@@ -0,0 +1,38 @@
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
|
||||
// 定义初始状态
|
||||
const initialState = {
|
||||
userInfo: null, // 用户信息初始为null
|
||||
isLoggedIn: false // 登录状态初始为false
|
||||
};
|
||||
|
||||
const userSlice = createSlice({
|
||||
name: 'user',
|
||||
initialState,
|
||||
reducers: {
|
||||
// 设置用户信息
|
||||
setUserInfo: (state, action) => {
|
||||
state.userInfo = action.payload;
|
||||
state.isLoggedIn = true;
|
||||
},
|
||||
// 清除用户信息
|
||||
clearUserInfo: (state) => {
|
||||
state.userInfo = null;
|
||||
state.isLoggedIn = false;
|
||||
},
|
||||
// 更新用户信息的部分字段
|
||||
updateUserInfo: (state, action) => {
|
||||
if (state.userInfo) {
|
||||
state.userInfo = {
|
||||
...state.userInfo,
|
||||
...action.payload
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 导出actions
|
||||
export const { setUserInfo, clearUserInfo, updateUserInfo } = userSlice.actions;
|
||||
|
||||
export default userSlice.reducer;
|
||||
Reference in New Issue
Block a user