反应路由器 2.0 哈希历史记录 - 如何删除查询



在模块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}>

最新更新