【无标题】

一:创建项目

超全面详细一条龙教程!从零搭建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>
相关推荐
嘘嘘出差7 小时前
ArcGIS切片方案记录bundle文件
arcgis
摆烂老大1 天前
SWAT| 水文 | SWAT模型(三):土壤数据库制备
arcgis·水文·swat模型
鸿业远图科技2 天前
分式注记种表达方式arcgis
python·arcgis
城市数据匠2 天前
31【干货】Arcgis属性表常用查询表达式实战大全
arcgis·gis·cad·国土空间规划·自然资源局
新中地GIS开发老师2 天前
【Cesium入门教程】第七课:Primitive图元
arcgis·信息可视化·gis开发·webgis·地理信息系统·地理信息科学
王孝点3 天前
geoserver发布arcgis瓦片地图服务(最新版本)
arcgis
没有梦想的咸鱼185-1037-16635 天前
【大语言模型ChatGPT4/4o 】“AI大模型+”多技术融合:赋能自然科学暨ChatGPT在地学、GIS、气象、农业、生态与环境领域中的应用
人工智能·python·机器学习·arcgis·语言模型·chatgpt·数据分析
新中地GIS开发老师6 天前
【Cesium入门教程】第五课:数据源
arcgis·遥感·gis开发·webgis·地理信息科学
没有梦想的咸鱼185-1037-16636 天前
【大模型ChatGPT+ArcGIS】数据处理、空间分析、可视化及多案例综合应用
人工智能·arcgis·chatgpt·数据分析
玩大数据的龙威7 天前
【ArcGIS技巧】用地块生成界址点去重、顺时针编号挂接DKBM属性
arcgis