【发布时间】:2019-10-17 13:30:18
【问题描述】:
我正在研究在 TypeScript 应用程序中使用 react 路由器。
如何在 SPA 中更新文档标题 (document.title) 和应用栏标题 (<span className="appbartitle">Home</span>) 以响应路由器导航?
import React from 'react';
import {BrowserRouter as Router, Switch, Route, Link} from "react-router-dom";
import './App.css';
const App: React.FC = () => {
return (
<div className="App">
<Router>
<div className="appbar">
<span className="appbartitle">Home</span>
<span className="menuItem"><Link to="/">Home</Link></span>
<span className="menuItem"><Link to="/about">About</Link></span>
<span className="menuItem"><Link to="/dashboard">Dashboard</Link></span>
</div>
<Switch>
<Route exact path="/"><Home /></Route>
<Route path="/about"><About /></Route>
<Route path="/dashboard"><Dashboard /></Route>
</Switch>
</Router>
</div>
);
}
export default App;
const Home = () => <div><h2>Home page</h2></div>;
const About = () => <div><h2>About page</h2></div>;
const Dashboard = () => <div><h2>Dashboard page</h2></div>;
【问题讨论】:
标签: reactjs typescript react-router