要利用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。