javascript - 使用 HashRouter 时以编程方式导航
问题描述
我在 react.js 应用程序中将 HashRouter 用于我的路由。然后我有一个执行以下操作的函数:
this.props.history.push('/somePath');
问题是,自从我开始使用 以来HashRouter
,当调用该函数时,页面并没有推送到该路径。
我登录了this.props.history
,并且push
在那里。
如何在使用时以编程方式导航HashRouter
?
注:我用过withRouter
这是代码:
import React, { Component } from 'react';
import { HashRouter, Route, Switch, Redirect, withRouter } from 'react-router-dom';
// Other imports
class App extends Component {
navigate = () => {
this.props.history.push('/route1');
console.log(this.props.history);
};
render() {
return (
<div className="App">
<Header />
<HashRouter>
<Switch>
<Route
path="/route1"
exact
render={() => (
<FirstRoute someSetting='setting1'/>
)}
/>
<Route
path="/route2"
exact
render={() => (
<SecondRoute anotherSetting='Really Cool'/>
)}
/>
<Route path="/404" component={NotFound} />
<Route exact path="/" render={() => <HomePage someSettings='Home Page' />} />
<Redirect to="/404" />
</Switch>
</HashRouter>
<BottomBar />
</div>
);
}
}
export default withRouter(App);
解决方案
推荐阅读
- ffmpeg - ffmpeg drawtext 支持藏文字体
- sql - SQL 在搜索下方查找结果 1 字段
- shell - 如何拆分路径以获取shell脚本中的目录
- javascript - 如何使用脚本仅隐藏一半图像?
- java - Java 多线程应用程序 - 在 Hive 上间歇性地获取“错误文件描述符”异常
- aws-sdk - AWS WorkDocs 开发工具包 - 如何搜索文件夹?
- javascript - 使用 JSON.parse 从魔兽日志 API 获取数据
- c++ - 如何正确指定 C++ 中的#include 路径以使您的程序可移植
- visual-c++ - 停止 CTreeCtrl 中的树项展开 - MFC
- javascript - 期望一个函数改变例如数组的长度