Skip to content

安装 ​

sh
# npm


<NolebasePageProperties />




npm install react-router-dom

# yarn
yarn add react-router-dom

# pnpm 
pnpm i react-router-dom

使用 ​

API文档 https://devdocs.io/react_router/

index.tsx ​

js
import * as React from 'react';
import { createRoot } from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import App from './App';

const rootElement = document.getElementById('root');
const root = createRoot(rootElement);

root.render(
	<BrowserRouter>
		<App />
	</BrowserRouter>
);
  • 用<BrowserRouter> 包住 根组件 App 后 , 我们就可以在每一个组件使用 React-Router

App.tsx ​

js
import * as React from 'react';
import './style.css';
import { Route, Routes } from 'react-router-dom';
import Login from './pages/Login';

export default function App() {
  return (
	<Routes>
		<Route path="/login" element={<Login />} />
	</Routes>
	);
}
  • Route 对应某个组件 , path 是路径
  • Link 类似 a 标签, 但不会发生 http 请求 , 而是加载对应的路由组件

贡献者

The avatar of contributor named as rover rover

页面历史

Power by nolebase