feat: 文件命名规则修改

This commit is contained in:
dayjoy
2024-10-22 14:20:24 +08:00
parent e47cf31c07
commit 2a25a41be7
12 changed files with 42 additions and 32 deletions

View File

@@ -10,7 +10,7 @@
- dev - dev
```shell ```shell[src](src)
yarn start yarn start
``` ```
@@ -19,7 +19,7 @@
```shell ```shell
yarn build yarn build
``` ```
## 路由和菜单 ## 路由和菜单
本工程采用配置化自动生成路由和菜单方案,在 `pages` 下的单个页面目录中配置 `meta.json` 文件,即可生成路由和菜单,具体配置如下: 本工程采用配置化自动生成路由和菜单方案,在 `pages` 下的单个页面目录中配置 `meta.json` 文件,即可生成路由和菜单,具体配置如下:
@@ -57,8 +57,11 @@ interface Meta {
在每个页面内,可以使用以下常用方法和全局变量: 在每个页面内,可以使用以下常用方法和全局变量:
``` ```
navigate页面跳转方法用法navigate('/path')
props页面 props props页面 props
params当前页面路由参数
search当前页面搜索参数
location当前页面路由信息
navigate页面跳转方法用法navigate('/path')
tocoStore全局 store tocoStore全局 store
tocoServices页面的请求 tocoServices页面的请求
tocoRefs页面上所有带 id 组件的 refs tocoRefs页面上所有带 id 组件的 refs
@@ -71,6 +74,7 @@ interface Meta {
```shell ```shell
src src
├── App.tsx # 路由逻辑,一般不需要修改 ├── App.tsx # 路由逻辑,一般不需要修改
├── Root.tsx # 页面根节点
├── app # store 主逻辑 ├── app # store 主逻辑
│   └── store.ts # 如果写了新的 store需要在这里引入 │   └── store.ts # 如果写了新的 store需要在这里引入
├── components # 组件放这里 ├── components # 组件放这里
@@ -90,10 +94,10 @@ interface Meta {
│   └── index.tsx # 可以在这里切换布局方案 │   └── index.tsx # 可以在这里切换布局方案
├── pages # 页面放这里 ├── pages # 页面放这里
│   ├── demo # 一个页面一个文件夹 │   ├── demo # 一个页面一个文件夹
│   │   ├── Demo.module.css # 页面样式 │   │   ├── index.module.css # 页面样式
│   │   ├── Demo.tsx # 页面逻辑 │   │   ├── index.tsx # 页面逻辑
│   │   ├── demoAPI.ts # 页面请求 │   │   ├── api.ts # 页面请求
│   │   ├── demoSlice.ts # 页面 store │   │   ├── slice.ts # 页面 store
│   │   └── meta.json # 页面元数据,用于生成路由和菜单,没有这个文件视为普通组件 │   │   └── meta.json # 页面元数据,用于生成路由和菜单,没有这个文件视为普通组件
│   └── test │   └── test
├── react-app-env.d.ts ├── react-app-env.d.ts

View File

@@ -1,5 +1,5 @@
import Layout from '@/layout'; import Layout from '@/layout';
import { toPascalCase } from '@/utils'; import withPage from '@/utils/withPage';
import { lazy } from 'react'; import { lazy } from 'react';
import { import {
createBrowserRouter, createBrowserRouter,
@@ -18,12 +18,11 @@ const parseMeta2Routes = (meta: Meta[]) => {
routes.push(...parseMeta2Routes(item._children)); routes.push(...parseMeta2Routes(item._children));
return; return;
} }
const Element = lazy( const Element = lazy(() => import(`@/pages/${item._fullpath}`));
() => import(`@/pages/${item._fullpath}/${toPascalCase(item._path)}`), const PageElement = withPage(Element);
);
const route: RouteObject = { const route: RouteObject = {
path: item._fullroute, path: item._fullroute,
element: <Element />, element: <PageElement />,
}; };
routes.push(route); routes.push(route);
}); });

5
template/src/Root.tsx Normal file
View File

@@ -0,0 +1,5 @@
const Root: React.FC<React.HTMLAttributes<HTMLDivElement>> = (props) => {
return <div {...props} />;
};
export default Root;

View File

@@ -1,4 +1,4 @@
import demoReducer from '@/pages/demo/demoSlice'; import demoReducer from '@/pages/demo/slice';
import { Action, configureStore, ThunkAction } from '@reduxjs/toolkit'; import { Action, configureStore, ThunkAction } from '@reduxjs/toolkit';
import commonReducer from './common'; import commonReducer from './common';

View File

@@ -1,7 +1,7 @@
import { useAppDispatch, useAppSelector } from '@/app/hooks'; import { useAppDispatch, useAppSelector } from '@/app/hooks';
import { User } from '@/pages/demo/demoAPI'; import { User } from '@/pages/demo/api';
import { fetchUsers, selectDemo } from '@/pages/demo/demoSlice'; import { fetchUsers, selectDemo } from '@/pages/demo/slice';
import withPage from '@/utils/withPage'; import Root from '@/Root';
import { EditTableColumnsType, Table } from '@df/toco-ui'; import { EditTableColumnsType, Table } from '@df/toco-ui';
import { useEffect } from 'react'; import { useEffect } from 'react';
@@ -40,8 +40,10 @@ const Demo = () => {
}, [dispatch]); }, [dispatch]);
return ( return (
<Table id="demoTable" value={users} columns={columns} editMode="none" /> <Root>
<Table id="demoTable" value={users} columns={columns} editMode="none" />
</Root>
); );
}; };
export default withPage(Demo); export default Demo;

View File

@@ -1,5 +1,5 @@
import { AppThunk, RootState } from '@/app/store'; import { AppThunk, RootState } from '@/app/store';
import { getUsers, User } from '@/pages/demo/demoAPI'; import { getUsers, User } from '@/pages/demo/api';
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; import { createSlice, PayloadAction } from '@reduxjs/toolkit';
export interface DemoState { export interface DemoState {
@@ -11,7 +11,7 @@ const initialState: DemoState = {
users: [], users: [],
}; };
export const demoSlice = createSlice({ export const slice = createSlice({
name: 'demo', name: 'demo',
initialState, initialState,
reducers: { reducers: {
@@ -21,7 +21,7 @@ export const demoSlice = createSlice({
}, },
}); });
export const { setUsers } = demoSlice.actions; export const { setUsers } = slice.actions;
export const fetchUsers = (): AppThunk => async (dispatch, getState) => { export const fetchUsers = (): AppThunk => async (dispatch, getState) => {
const data = await getUsers(); const data = await getUsers();
@@ -30,4 +30,4 @@ export const fetchUsers = (): AppThunk => async (dispatch, getState) => {
export const selectDemo = (state: RootState) => state.demo; export const selectDemo = (state: RootState) => state.demo;
export default demoSlice.reducer; export default slice.reducer;

View File

@@ -1,7 +1,7 @@
import { useAppSelector } from '@/app/hooks'; import { useAppSelector } from '@/app/hooks';
import withPage from '@/utils/withPage'; import Root from '@/Root';
import { useEffect } from 'react'; import { useEffect } from 'react';
import * as tocoServices from './testAPI'; import * as tocoServices from './api';
const Test = (props) => { const Test = (props) => {
const tocoStore = useAppSelector((state) => state); const tocoStore = useAppSelector((state) => state);
@@ -16,7 +16,7 @@ const Test = (props) => {
}, [props, tocoStore]); }, [props, tocoStore]);
return ( return (
<div> <Root>
<h1>Store</h1> <h1>Store</h1>
<p>the primary color is: {tocoStore.common.primaryColor}</p> <p>the primary color is: {tocoStore.common.primaryColor}</p>
<h1>Service</h1> <h1>Service</h1>
@@ -24,8 +24,8 @@ const Test = (props) => {
<div> <div>
<p>children page</p> <p>children page</p>
</div> </div>
</div> </Root>
); );
}; };
export default withPage(Test); export default Test;

View File

@@ -1,7 +1,7 @@
import { useAppSelector } from '@/app/hooks'; import { useAppSelector } from '@/app/hooks';
import withPage from '@/utils/withPage'; import Root from '@/Root';
import { useEffect } from 'react'; import { useEffect } from 'react';
import * as tocoServices from './xxxAPI'; import * as tocoServices from './api';
const Xxx = (props) => { const Xxx = (props) => {
const tocoStore = useAppSelector((state) => state); const tocoStore = useAppSelector((state) => state);
@@ -16,10 +16,10 @@ const Xxx = (props) => {
}, [props, tocoStore]); }, [props, tocoStore]);
return ( return (
<div> <Root>
<h1>xxx</h1> <h1>xxx</h1>
</div> </Root>
); );
}; };
export default withPage(Xxx); export default Xxx;