【无标题】

一:创建项目

超全面详细一条龙教程!从零搭建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>
相关推荐
智航GIS2 天前
ArcGIS大师之路500技---070对齐边工具
arcgis
智航GIS2 天前
ArcGIS Python零基础脚本开发教程---10.3 Exists函数
开发语言·python·arcgis
有个人神神叨叨2 天前
pnpm cnpm npm 对比
前端·arcgis·npm
有个人神神叨叨2 天前
node、npm 和 npx 的关系
前端·arcgis·npm
NBhhbYyOljP4 天前
人工势场与领航跟随的Matlab仿真
arcgis
智航GIS4 天前
ArcGIS Python零基础脚本开发教程---1.1 Describe 函数
开发语言·python·arcgis
zhz52144 天前
ArcGIS实习教程
arcgis·地理信息·空间数据
小艳加油4 天前
ArcGIS赋能水文水环境保护:从基础操作到高级分析,涵盖数据库构建、空间插值、水文模拟与水环境容量计算的综合技能
arcgis·水文水资源·地理信息系统
智航GIS5 天前
ArcGIS大师之路500技---069整形边工具
arcgis
瑞瑞大大5 天前
arcgis中判断栅格数据有没有0值
arcgis·栅格0值