【无标题】

一:创建项目

超全面详细一条龙教程!从零搭建React项目全家桶(上篇) - 知乎

1.创建一个文件夹,shift+鼠标右键选择在此处打开powershell

2.为了加速npm下载速度,先把npm设置为淘宝镜像地址。

npm config set registry http://registry.npm.taobao.org/

3.执行下面的npm安装react,那么基础的项目就搭建好了

npx create-react-app react-app

4.使用下列npm运行项目

npm start

二:精简项目

删除例如App.css、App.test.js、index.css、logo.svg、reportWebvitals.js、setupTests.js等文件,

保留核心index.js、App.js;优化成下图

三.创建文件并安装路由

1.基本文件格式

2.安装路由 npm install react-router-dom

  1. router文件夹下index.js的模板
javascript 复制代码
// 引入组件
import Login from "../views/login";
// 注册路由数组
const routes = [
  {
    // 首页默认是/
    path: "/",
    element: <Login></Login>,
  }
];
// 导出
export default routes;
  1. views文件夹下面的页面的基本模板结构
javascript 复制代码
import React from "react";
export default function login() {
  return <div>这是子页面</div>;
}

5.配置 App.js 使用路由 : 在 App.js 中使用 useRoutes 来注册路由:

javascript 复制代码
import { useRoutes } from 'react-router-dom';
import routes from './router';
 
function App() {
  return (
    <div>
      {/* 注册路由 */}
      {useRoutes(routes)}
    </div>
  );
}
 
export default App;

6.配置 index.js 引入 HashRouter : 在 index.js 中,使用 HashRouter 包裹 App 组件,配置路由:

javascript 复制代码
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import { HashRouter } from 'react-router-dom'; // 导入 HashRouter
 
const root = ReactDOM.createRoot(document.getElementById('root'));
 
root.render(
  <HashRouter>
    <App />
  </HashRouter>
);

7.index.html替换为

javascript 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1"
/>
<meta name="theme-color" content="#000000" />
<meta name="description" content="Web site created using createreact-app" />
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
相关推荐
你是一个铁憨憨3 天前
ArcGIS定向影像(1)——非传统影像轻量级解决方案
arcgis·gis·影像·定向影像
QQ3596773453 天前
ArcGIS Pro实现基于 Excel 表格批量创建标准地理数据库(GDB)——高效数据库建库解决方案
数据库·arcgis·excel
阿智@115 天前
推荐使用 pnpm 而不是 npm
前端·arcgis·npm
GIS思维5 天前
ArcGIS(Pro)在线地图服务被禁?提示感叹号?应急方案来了——重新正常显示
arcgis·arcgispro
bug总结5 天前
多楼层室内定位可视化 Demo(A*路径避障)
arcgis
草木红6 天前
express 框架基础和 EJS 模板
arcgis·node.js·express
wanzhong23337 天前
ArcGIS学习-17 实战-密度分析
学习·arcgis
树谷-胡老师8 天前
公元前3400年至公元2024年全球国家地理边界演变数据集
数据库·arcgis·信息可视化
wanzhong23339 天前
ArcGIS学习-20 实战-县域水文分析
学习·arcgis
树谷-胡老师9 天前
2005–2021年中国城市级终端能源消费(含可再生能源)综合数据集
arcgis·能源