在React项目中配置路由需要先安装react-router-dom库,然后在项目中创建一个路由组件,并在App.js中引入该组件。具体步骤如下:
npm install react-router-dom
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Home from './components/Home';
import About from './components/About';
const Routes = () => {
return (
<Router>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
</Switch>
</Router>
);
};
export default Routes;
import React from 'react';
import Routes from './routes';
function App() {
return (
<div>
<Routes />
</div>
);
}
export default App;
import React from 'react';
const Home = () => {
return (
<div>
<h1>Home Page</h1>
</div>
);
};
export default Home;
import React from 'react';
const About = () => {
return (
<div>
<h1>About Page</h1>
</div>
);
};
export default About;
通过以上步骤,就可以在React项目中成功配置路由,并实现页面的跳转和显示。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。