您好,登錄后才能下訂單哦!
在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。