React基础框架搭建5-路由配置:react+router+redux+axios+Tailwind+webpack

复制代码
npm install react-router-dom

1、创建文件

在 src 目录下创建一个 router 文件夹,并在其中创建 AppRouter.jsx 文件

复制代码
//src/router/AppRouter.jsx

import React from 'react';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import Home from '../views/Home/Home'; // 事例页面
import About from '../views/About/About'; // 事例About组件

const AppRouter = () => {
    return (
        <Router>
            <Routes>
                <Route path="/" element={<Home />} />
                <Route path="/about" element={<About />} /> 
                {/* 其他路由 */}
            </Routes>
        </Router>
    );
};

export default AppRouter;

2、 创建示例页面

在 src/views 目录下创建 Home 文件夹,并在其中创建 Home.jsx 文件

复制代码
//src/views/Home/Home.jsx

import React from 'react';
import { Link } from 'react-router-dom';

const Home = () => {
    return (
        <div>
            <h1>欢迎来到 ReactBase 框架</h1>
            <p>这是一个基于 React 的前端框架示例。</p>
            <br />
            <hr />
            <br />
            <Link to="/about">
                <button>前往关于页面</button>
            </Link>
        </div>
    );
};

export default Home;

Home添加样式版本:

复制代码
import React from 'react';
import { Link } from 'react-router-dom';

const Home = () => {
    return (
        <div className="flex flex-col items-center justify-center h-screen">
            <h1 className="text-4xl font-bold">欢迎来到 ReactBase 框架</h1>
            <p className="mt-4 text-lg">这是一个基于 React 的前端框架示例。</p>
            <br />
            <Link to="/about">
                <button className="mt-4 px-4 py-2 bg-blue-500 text-white rounded">前往关于页面</button>
            </Link>
        </div>
    );
};

export default Home;

在 src/views 目录下创建 About 文件夹,并在其中创建 About.jsx 文件

复制代码
//src/views/About/About.jsx

import React from 'react';

const About = () => {
    return (
        <div>
            <h1>关于我们</h1>
            <p>这是关于页面的内容。</p>
        </div>
    );
};

export default About; 

3、引入 Redux Store 和路由组件

更新src/index.js

复制代码
import React from 'react';
import ReactDOM from 'react-dom/client';
import { Provider } from 'react-redux';
import store from './store';
import AppRouter from './router/AppRouter';
import './index.css';
import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <Provider store={store}>
      <AppRouter />
    </Provider>
  </React.StrictMode>
);

reportWebVitals();
相关推荐
Flynt2 小时前
AI 生成的界面,能放心点吗?我把 Google A2UI 的三层防线拆开测了一遍
前端·google·agent
前端混子3 小时前
我用 AI 做了个「禾月小历」,一套代码跑微信小程序和 H5
前端
IT_陈寒3 小时前
Java实战技巧总结:当你的finally块没有执行时会发生什么?
前端·人工智能·后端
小帅不太帅3 小时前
一年一次黑五,为了买软件真省钱,我做了个开源小站
前端·人工智能·github
JavaGuide5 小时前
刚刚,阿里版 Codex 上线了匿名模型 HungerG,无限 Token!
前端·后端
zhanlang02175 小时前
HTML 零基础入门到企业实战全套教程【第九章 9.3】移动端 HTML 基础适配入门
前端·html
用户69371750013845 小时前
AI 帮我写了大半年的代码,我却越来越不会写代码了
前端·后端·github
skywalk81636 小时前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness
彭亚川Allen6 小时前
从零手搓 RAG:用 Elasticsearch 存储与检索向量
前端·后端·面试
jerrywus6 小时前
15 个可以直接安装的 AI Agent Skills:按场景挑
前端·agent·claude