React是一个流行的JavaScript库,用于构建用户界面。React Router是React的官方路由库,用于管理应用程序的路由。
要使React路由路径为1/2/3,可以按照以下步骤进行操作:
npm install react-router-dom
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
const Component1 = () => <h1>Component 1</h1>;
const Component2 = () => <h1>Component 2</h1>;
const Component3 = () => <h1>Component 3</h1>;
Router
组件和Route
组件来设置路由。const App = () => {
return (
<Router>
<div>
<nav>
<ul>
<li>
<Link to="/1">Component 1</Link>
</li>
<li>
<Link to="/2">Component 2</Link>
</li>
<li>
<Link to="/3">Component 3</Link>
</li>
</ul>
</nav>
<Route path="/1" component={Component1} />
<Route path="/2" component={Component2} />
<Route path="/3" component={Component3} />
</div>
</Router>
);
};
App
组件。ReactDOM.render(<App />, document.getElementById('root'));
现在,当用户访问路径为/1
、/2
或/3
时,React将渲染相应的组件。
领取专属 10元无门槛券
手把手带您无忧上云