创建Umi3
// 创建umi3
yarn create @umijs/umi-app 或者 npm create @umijs/umi-app
// 结束后按照依赖
npm i 或者 yarn
配置式路由和约定式路由
// 当.umirc.ts存在routes, 则默认为配置式路由,注释掉就默认为约定式路由
routes: [ // 配置式路由
{ path: '/', component: '@/pages/index' },
{ path: '/test', component: '@/pages/test/index' },
]
在Umi内使用AntDesign
// umi默认集成好了因此可以直接使用
import {Button} from 'antd';
// 配置要开启后样式才能生效, 配置项要放全局配置: .umirc.ts
import { defineConfig } from 'umi';
export default defineConfig({
nodeModulesTransform: {
type: 'none',
},
antd: {// 配置开启antd组件库的使用
dark: false // 开启暗黑主题
},
routes: [ // 配置式路由
{ path: '/', component: '@/pages/index' },
{ path: '/test', component: '@/pages/test/index' },
],
fastRefresh: {},
});
拆分配置.umirc.ts(因为路由或者其他配置可能会很多,拆分出去后不会显得一个文件的内容很多)
// 在根目录下(也就是与src同级)创建config文件夹以及在文件夹下创建config.js,并将.umirc.ts的代码全部复制过去只保留路由即可,.umirc.ts的优先级比config的优先级更高。
// 继续在config文件夹下创建routes.js
config.js代码如下:
import { defineConfig } from 'umi';
import routes from './routes.js'
export default defineConfig({
nodeModulesTransform: {
type: 'none',
},
antd: {// 配置开启antd组件库的使用
dark: false // 开启暗黑主题
},
routes,
fastRefresh: {},
});
routes.js代码如下:
export default [ // 配置式路由
{ path: '/', component: '@/pages/index' },
{ path: '/test', component: '@/pages/test/index' },
]
开启项目视图骨架配置
// 在config.js内添加layout
export default defineConfig({
nodeModulesTransform: {
type: 'none',
},
antd: {// 配置开启antd组件库的使用
dark: false // 开启暗黑主题
},
routes,
fastRefresh: {},
layout: { // 开启项目视图骨架配置
// 支持任何不需要dom的
name: 'Ant Design',
locale: true,
layout: 'side'
}
});
// 然后routes.js内路由添加name
export default [ // 配置式路由
{
path: '/',
component: '@/pages/index',
name: '首页'
},
{
path: '/test',
component: '@/pages/test/index',
name: '测试'
},
// {
// path: '/stu',
// name: '学院管理',
// routes: [
// {
// path: '/stu/list',
// component: '@/pages/stu/list',
// name: '学员列表'
// },
// {
// path: '/stu/pub',
// component: '@/pages/stu/pub',
// name: '学员录入'
// }
// ]
// }
]
// 页面点击也需要跳转的写法如下(可以使用Link也可以使用history):
import styles from './index.less';
import {Button} from 'antd';
import {Link, history} from 'umi';
export default function IndexPage() {
return (
<div>
<h1 className={styles.title}>
<Link to="/">test</Link>
</h1>
<Button onClick={() => {
// history.push({pathname: '/'}); 两种都可以
history.push('/')
}} type="dashed">按钮</Button>
</div>
);
}
完成这些后可以直接进行开发了