Skip to content

Commit 5ed2bed

Browse files
committed
feat: 添加route的相关store
1 parent 5c696fc commit 5ed2bed

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/store/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import type { Action, ThunkAction } from '@reduxjs/toolkit';
33

44
import { appSlice } from './slice/app';
55
import { authSlice } from './slice/auth';
6+
import { routeSlice } from './slice/route';
7+
import { tabSlice } from './slice/tab';
68
import { themeSlice } from './slice/theme';
79

810
// `combineSlices` automatically combines the reducers using
911
// their `reducerPath`s, therefore we no longer need to call `combineReducers`.
10-
const rootReducer = combineSlices(appSlice, authSlice, themeSlice);
12+
const rootReducer = combineSlices(appSlice, authSlice, themeSlice, routeSlice, tabSlice);
1113

1214
// Infer the `RootState` type from the root reducer
1315
export type RootState = ReturnType<typeof rootReducer>;

src/store/slice/route/index.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import type { PayloadAction } from '@reduxjs/toolkit';
2+
3+
import { createAppSlice } from '../../createAppSlice';
4+
5+
interface InitialStateType {
6+
cacheRoutes: string[];
7+
removeCacheKey: string | null;
8+
routeHome: string;
9+
}
10+
11+
const initialState: InitialStateType = {
12+
/** - 需要进行缓存的页面 */
13+
cacheRoutes: [],
14+
/** - 需要删除的缓存页面 */
15+
removeCacheKey: null,
16+
/** - 首页路由 */
17+
routeHome: import.meta.env.VITE_ROUTE_HOME
18+
};
19+
20+
export const routeSlice = createAppSlice({
21+
initialState,
22+
name: 'route',
23+
reducers: create => ({
24+
resetRouteStore: create.reducer(() => initialState),
25+
setCacheRoutes: create.reducer((state, { payload }: PayloadAction<string[]>) => {
26+
state.cacheRoutes = payload;
27+
}),
28+
setRemoveCacheKey: create.reducer((state, { payload }: PayloadAction<string | null>) => {
29+
state.removeCacheKey = payload;
30+
})
31+
}),
32+
selectors: {
33+
selectCacheRoutes: route => route.cacheRoutes,
34+
selectRemoveCacheKey: route => route.removeCacheKey
35+
}
36+
});
37+
38+
export const { resetRouteStore, setCacheRoutes, setRemoveCacheKey } = routeSlice.actions;
39+
40+
export const { selectCacheRoutes, selectRemoveCacheKey } = routeSlice.selectors;

0 commit comments

Comments
 (0)