React@16.x(40)路由v5.x(5)常见应用场景(2)- 实现类似 vue 的路由模式

1,vue-router

vue 的路由配置文件,

// src/router/index.ts
const routes = [
    {
        path: "/news",
        children: [
            { path: "", component: NewsView },
            { path: "detail", component: NewsDetail },
            { path: "search", component: NewsSearch },
        ],
    },
    {
        path: "/goods",
        component: GoodsView,
        children: [
            { path: "detail", component: GoodsDetail },
            { path: "search", component: GoodsSearch },
        ],
    },
    { path: "/", component: HomeView },
];

App.vue 中使用 RouterView 即可渲染匹配到的路由:

<template>
    <RouterView></RouterView>
</template>

另外,注意 newsgoods 路由的区别:

  • news 相关的3个路由页面是相互独立的,只是逻辑上有关系。
  • goods 路由的写法,需要在 GoodsView 组件内也使用 RouterView 才能访问到路由 goods/detailgoods/search 对应的页面。
<template>
	<div>goods主页面</div>
    <RouterView></RouterView>
</template>

2,React 模拟实现

使用和 vue 相同的配置文件routeConfig.js

App.jsx

import { BrowserRouter as Router, Switch, Link } from "react-router-dom";
import RootRoute from "./RootRoute";

export default function App() {
    return (
        <Router>
            <Link to="/">首页</Link>
            <Link to="/news">新闻页</Link>
            <Link to="/goods">商品页</Link>
            <Switch>
                <RootRoute></RootRoute>
            </Switch>
        </Router>
    );
}

关键实现:RootRoute.js

import React from "react";
import { Route } from "react-router-dom";
import routeConfig from "./routeConfig";

export default function RootRoute() {
    return getRoutes(routeConfig, "");
}

function getRoutes(routes, basePath) {
    if (!Array.isArray(routes)) {
        return null;
    }
    return routes.map((route) => {
        const { path, component: Component, children, ...rest } = route;
        const newPath = `${basePath}${path}`;
        // 适配 news 路由的方式
        if (Component) {
            return (
                <Route
                    key={newPath}
                    path={newPath}
                    exact={["", "/"].includes(path)}
                    {...rest}
                    render={(values) => {
                        return <Component {...values}>{getRoutes(children, newPath)}</Component>;
                    }}
                ></Route>
            );
        } else {
            return getRoutes(children, newPath);
        }
    });
}

News.jsx

export default function News() {
    return (
        <div>
            <div>News页面</div>
            <Link to="/news/detail">详情</Link>
            <Link to="/news/search">查询</Link>
        </div>
    );
}

Goods.jsx,其中 props.childrenRootRoute.js 遍历子路由渲染出对应的 <Route> 组件。

export default function Goods(props) {
    return (
        <div>
            <div>Goods页面</div>
            <Link to="/goods/detail">goods详情</Link>
            <Link to="/goods/search">goods查询</Link>
            <div>{props.children}</div>
        </div>
    );
}

效果:

在这里插入图片描述


以上。

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

下雪天的夏风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值