我正在尝试在CEP扩展中使用react-router。我的路由看起来像这样:
let prefix = decodeURIComponent(window.location.pathname).replace("index.html", "")
<Router>
<Switch>
<Route exact path={prefix + "index.html"} component={MainComponent} />
<Route path={prefix + "other/:otherId"} component={OtherComponent} />
</Switch>
</Router>
这似乎是欺骗路由器接受扩展位置的唯一方法 - 因为它有一个document.//ocation为'file://path/to/cep/extention/index.html'。我现在面临的问题是,这只适用于Mac - 但始终无法匹配Windows上的任何路径。我怀疑这是因为窗口上的位置看起来像:'file:/// C:/ Program%20Files%20(x86)/Common%20Files/...be/CEP/extensions/extension-name/index.html'和'C:'混淆了路由器?
有没有办法欺骗路由器接受这种位置URI?
简单的解决方案 - 使用HashRouter而不是BrowserRouter。这也允许使用正常路径:
import { Route, HashRouter as Router, Switch } from 'react-router-dom'
<Router >
<Switch>
<Route exact path="/" component={MainComponent} />
<Route path="/other/:otherId" component={OtherComponent} />
</Switch>
</Router>