antd pro + umi4 + tailwindcss 开发总结
项目初始化
如果不需要全球化可以使用 npm run i18n-remove ,formatMessage({id:somevar}) 这种动态的代码可能无法分析并删除,有些得手动删除。
在 src/models 目录下新建文件,文件名会成为 model 的 namespace. 允许使用 ts, js, tsx(推荐), jsx(不推荐) 四种后缀。useModel(‘文件名’)的数据可全局使用
src/models/demo.ts
1 2 3 4 5 6 7
| export default () => { const [data, setData] = useState('hello') return { data, setData } }
|
可以在预览界面中拷贝设置覆盖到 config\defaultSettings.ts 中来修改配置。
config/proxy.ts中配置代理
引入tailwindcss
config/config.ts
1 2 3 4
| export default defineConfig({ ..... extraPostCSSPlugins: [require('tailwindcss')], });
|
src/global.less 最上面
1 2 3
| @import 'tailwindcss/base'; @import 'tailwindcss/components'; @import 'tailwindcss/utilities';
|
与src平级添加 tailwind.config.js
1 2 3 4 5 6 7
| module.exports = { content: [ './src/**/*.html', './src/**/*.tsx', './src/**/*.jsx', ], }
|
数据初始化 ( 全局可用initialState )
src/app.tsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| type Menu = { component?: string; icon?: string; name?: string; path?: string; }; export async function getInitialState(): Promise<{ settings?: any; menuData?: Menu[]; userInfo?: any, }> { let data: Data = {}; let menu: any = [] const {location} = history console.log('pathname', location.pathname) try { await 调用接口初始化数据, } catch (error) {} return { settings: defaultSettings as Partial<LayoutSettings>, menuData: menu, userInfo: data?.examUser, }; }
|
动态表单、自定义渲染菜单
src/app.tsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| export const layout: any = ({ initialState, setInitialState }) => { const { location } = history; const { pathname } = location; return { menu: { params: { menuData: initialState?.menuData }, request: async (params: any, defaultMenuData: any) => { return 需要配置的菜单 }, }, menuItemRender: (itemProps) => { return ( <div className="flex" onClick={() => { history.push(itemProps.path); }} > <Space> {itemProps?.icon && ( <img style={{ width: 16, height: 16 }} src={itemProps?.icon} alt="" /> )} <div>{itemProps.name}</div> </Space> </div> ); }, childrenRender: (children) => { return ( <> {children} {/* <SettingDrawer disableUrlParams enableDarkTheme settings={initialState?.settings} onSettingChange={(settings) => { setInitialState((preInitialState) => ({ ...preInitialState, settings, })); }} /> */} </> ); }, ...initialState?.settings, }; };
|
动态路由 ( 如果不需要动态生成路由,在config/routes.ts里面配置即可,不需要加下面的函数了 。)
src/app.tsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import React from 'react'; export function patchClientRoutes({ routes }) { routes.map((res) => { if(res.path === '/') { path: '/foo', element: React.createElement(require('@/pages/OldPage').default), } }) routes.push( path: '/foo', element: React.createElement(require('@/pages/OldPage').default), ) }
|
部署相关配置
config/config.ts
1 2 3 4 5 6 7 8
| export default defineConfig({ publicPath: process.env.NODE_ENV === 'production' ? './' : '/' , hash: true, history: { type: 'hash' }, ..... });
|