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: {},
});
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值