gpt4 book ai didi

javascript - React Router browserHistory 在本地工作,而不是在生产环境中工作

转载 作者:行者123 更新时间:2023-11-29 10:09:12 27 4
gpt4 key购买 nike

每当我在本地使用 browserHistory 时,我都没有问题,但是当我在发布它之前对其进行测试时,我得到一个包含错误的空白页面。因此,当我用 hashHistory 替换 browserHistory 时,一切正常,但我丢失了漂亮的 url。

Uncaught SecurityError: Failed to execute 'replaceState' on 'History': A history state object with URL 'file:///Users/somefile/work/someproject/index.html' cannot be created in a document with origin 'null' and URL.

const Routes = (
<Router history={browserHistory}>
<Route path="/" component={Login} />
<Route path="/content" component={App} >
<Route path="/component1" component={component1} />
<Route path="/component2" component={component2} />
<Route path="/component3" component={component3} />
</Route>
</Router>
);

在我的 index.js 中

const createStoreWithMiddleware = applyMiddleware(ReduxPromise)(createStore);
const store = createStoreWithMiddleware(reducers);

ReactDOM.render(
<Provider store={store}>
{Routes}
</Provider>,
document.querySelector('.react-internal')
)

在我的 app.js 中

export default class App extends Component {

render() {
return (
<div>
<Header />
<div className="container-fluid">
{this.props.children}
</div>
</div>
);
}
}

和我的 webpack 文件

var webpack = require('webpack');

module.exports = {
devtool:'source-map',
entry: [
'./src/index.js',
],
output: {
path: __dirname,
publicPath: '/',
filename: 'bundle.js'
},
module: {
loaders: [
{
exclude: /node_modules/,
loaders: ['react-hot','babel']
},
{ test: /\.css$/, loader: "style!css" },
{
test: /\.(jpe?g|png|gif|svg)$/i,
loaders: [
'file?hash=sha512&digest=hex&name=[hash].[ext]',
'image-webpack?bypassOnDebug&optimizationLevel=7&interlaced=false'
]
}
]
},
resolve: {
extensions: ['', '.js', '.jsx']
},

devServer: {
port:8080,
historyApiFallback: true,
contentBase: './'
}
};

最佳答案

我必须添加一个基本 url 并链接到该文件夹​​。我认为这完全取决于您的部署方式

import { createHistory } from 'history'


const history = useRouterHistory(createHistory)({
basename: '/projectFolder'
})
const Routes = (
<Router history={history} >

<Route path="/" component={Login} />
<Route path="/content" component={App} >
<Route path="/component1" component={component1} />
<Route path="/component2" component={component2} />
<Route path="/component3" component={component3} />
</Route>
</Router>
);

关于javascript - React Router browserHistory 在本地工作,而不是在生产环境中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36735484/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com