react中路由的history与hash
在 React 中,路由是通过 `react-router` 库来实现的。`react-router` 提供了多种不同的路由方式,其中包括使用 `hash` 和 `history`。
在使用 `hash` 路由时,路由信息会被添加到 URL 的哈希部分(即 `#` 后面)。例如,对于路由 `/home`,其对应的 URL 将会是 ` 路由的实现是通过浏览器的原生 `window.location.hash` 属性。
jsx
import { HashRouter, Route } from 'react-router-dom';
const App = () => (
  <HashRouter>
    <Route exact path="/" component={Home} />
    <Route path="/about" component={About} />
  </HashRouter>
);
react router v6 文档使用 `history` 路由时,路由信息会被添加到 URL 的路径中。例如,对于路由 `/home`,其对应的 URL 将会是 ` 路由的实现是通过 `react-router` 提供的 `history` 对象。
jsx
import { Router, Route } from 'react-router-dom';
import { createBrowserHistory } from 'history';
const history = createBrowserHistory();
const App = () => (
  <Router history={history}>
    <Route exact path="/" component={Home} />
    <Route path="/about" component={About} />
  </Router>
);
无论是使用 `hash` 还是 `history` 路由,它们都允许你在 React 应用中进行页面之间的导航和路由。选择哪种方式取决于你的项目需求和个人喜好。