2016-10-03 103 views
0

我试图让我的反应应用程序在IE Edge浏览器中的文件系统上工作。到目前为止,我已经得到了这一点。如何获取React路由器基本名称

import { createHistory, useBasename } from 'history' 
let appHistory = useBasename(createHistory)({ 
    basename: '/build' 
}); 
<Router history={appHistory}> 
<Route path={window.location.pathname} component={App}> 

而且这种方式实际上,我可以看到一个页面渲染,但路线不工作 我也得在边缘浏览器的文件路径:/// C:/ C:/ ...等。为什么它有两个C:/?

在Chrome中我得到这个错误

未捕获抛出:DOMException:未能执行 '历史记录' 'pushState的':与URL一个 历史状态对象 '文件:/// C:/编译/窗口'不能是 在源文件'null'和URL 'file:/// C:/src/myproject/build/index.html'中创建。

+0

也许你从指导得到这个代码,我非常错误的,但我觉得路径设置为'window.location.pathname'不应该工作。你应该指定一个字符串作为你的路径,例如:'path = {'/'}' –

+0

实际上它只适用于文件系统中的window.location.pathname。现在唯一的问题是pushstate不起作用。 –

回答

2

您应该使用useRouterHistory而不是useBaseName

import { Router, Route, useRouterHistory } from 'react-router' 
import createBrowserHistory from 'history/lib/createBrowserHistory' 

const history = useRouterHistory(createBrowserHistory)({ 
    basename: '/dist' 
}); 

<Router history={history}> 
    <Route path="/" component={App} /> 
</Router>