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

目录

1,vue-router

vue 的路由配置文件,

js 复制代码
// 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 即可渲染匹配到的路由:

html 复制代码
<template>
    <RouterView></RouterView>
</template>

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

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

2,React 模拟实现

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

App.jsx

js 复制代码
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

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

js 复制代码
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> 组件。

js 复制代码
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>
    );
}

效果:


以上。

相关推荐
蓝婷儿26 分钟前
每天一个前端小知识 Day 28 - Web Workers / 多线程模型在前端中的应用实践
前端
琹箐26 分钟前
Ant ASpin自定义 indicator 报错
前端·javascript·typescript
小小小小小惠30 分钟前
Responsetype blob会把接口接收的二进制文件转换成blob格式
前端·javascript
爱电摇的小码农30 分钟前
【深度探究系列(5)】:前端开发打怪升级指南:从踩坑到封神的解决方案手册
前端·javascript·css·vue.js·node.js·html5·xss
kymjs张涛1 小时前
零一开源|前沿技术周报 #7
android·前端·ios
爱编程的喵1 小时前
React入门实战:从静态渲染到动态状态管理
前端·javascript
Tttian6221 小时前
npm init vue@latestnpm error code ETIMEDOUT
前端·vue.js·npm
患得患失9491 小时前
【前端】【组件库开发】【原理】【无框架开发】现代网页弹窗开发指南:从基础到优化
前端
唐叔在学习1 小时前
不用装插件!3轮对话,我用油猴脚本+AI复刻了掘金闪念笔记,真香!
javascript·浏览器
AliciaIr1 小时前
深入React事件机制:解密“合成事件”与“事件委托”的底层奥秘
javascript·react.js