您好,登錄后才能下訂單哦!
要利用TypeScript進行React路由配置,可以使用React Router庫。以下是一個簡單的示例:
npm install react-router-dom
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Home from './Home';
import About from './About';
const Routes: React.FC = () => {
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';
const App: React.FC = () => {
return (
<div>
<h1>My React App</h1>
<Routes />
</div>
);
};
export default App;
import React from 'react';
const Home: React.FC = () => {
return <h2>Home</h2>;
};
export default Home;
import React from 'react';
const About: React.FC = () => {
return <h2>About</h2>;
};
export default About;
http://localhost:3000
可以看到Home頁面,訪問http://localhost:3000/about
可以看到About頁面。通過以上步驟,就可以利用TypeScript進行React路由配置。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。