umi 配置式路由(基于React)

编程入门 行业动态 更新时间:2024-10-28 10:21:25

umi 配置式<a href=https://www.elefans.com/category/jswz/34/1771390.html style=路由(基于React)"/>

umi 配置式路由(基于React)

umi 中的路由分类两种:

配置式路由:开发者需要单独创建一个配置文件,通过各种属性或方法来实现对路由规则的配置;

约定式路由:开发者不需要手动配置,只需要按照 umi 的规则来创建路由目录和文件,umi 会自动根据文件目录将路由配置解析出来;

当项目中的路由结构比较简单,就可以使用约定式路由。如果是比较复杂的项目,都建议使用配置式路由。

一、路由模式

umi 项目中的路由模式默认是 history 模式,可以通过 history 属性来切换路由模式:

import { defineConfig } from 'umi';export default defineConfig({nodeModulesTransform: {type: 'none',},routes: [{ path: '/', component: '@/pages/index' },],// 路由模式history: {type: 'hash'},fastRefresh: {},
});

二、配置一级路由

路由的配置,在项目根目录中的 .umirc.ts 文件中进行:

import { defineConfig } from 'umi';export default defineConfig({nodeModulesTransform: {type: 'none',},routes: [{ path: '/', component: '@/pages/index' },{ path: '/my', component: '@/pages/my' },{ path: '/friend', component: '@/pages/friend' },],fastRefresh: {},
});

三、配置二级路由

import { defineConfig } from 'umi';export default defineConfig({nodeModulesTransform: {type: 'none',},routes: [{ path: '/', component: '@/pages/index',// 配置子路由routes: [{ path: '/discover', component: '@/pages/discover' },{ path: '/discover/topList', component: '@/pages/topList' },]},{ path: '/my', component: '@/pages/my' },{ path: '/friend', component: '@/pages/friend' },],history: {type: 'hash'},fastRefresh: {},
});

更多推荐

umi 配置式路由(基于React)

本文发布于:2024-03-06 18:37:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1716075.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   umi   React

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!