在模块history
中,我可以通过以下方式删除查询:
import createBrowserHistory from 'history/lib/createHashHistory';
const history = createBrowserHistory({ queryKey: false });
现在我react-router 2.0
从中获取历史:
import { Router, Route, hashHistory } from 'react-router';
如何清理 URL 并删除查询?
基于 react-router v2.0.0 升级指南:
import { Router, useRouterHistory } from 'react-router'
import { createHashHistory } from 'history'
// useRouterHistory creates a composable higher-order function
const appHistory = useRouterHistory(createHashHistory)({ queryKey: false })
<Router history={appHistory}/>
如您所见,createHashHistory
是从history
包中导入的,因此您必须安装它。或者你可以import { createHashHistory } from 'react-router/node_modules/history'
(因为history
现在是react-router
的正常依赖关系)
这很好用,谢谢。
如果有人正在使用浏览器(像我一样),代码应如下所示:
var useRouterHistory = require("react-router/lib/useRouterHistory");
var createHashHistory = require("react-router/node_modules/history/lib/createHashHistory");
var appHistory = useRouterHistory(createHashHistory)({ queryKey: false });
// finally add it to the router
<Router history={appHistory}>