react-router v2.0 browserHistory 不工作 [英] react-router v2.0 browserHistory not working
问题描述
目前使用的是 react-route v2.0.
Currently using react-route v2.0.
我确定我正在寻找一些东西,但我能够让 hashHistory 工作,但是在实现 browserHistory 时,我收到 404 错误消息,因为我试图删除 url 中的 hashHistory 并且我想知道解决方法生产.
I am sure I am over looking something but I am able to get hashHistory working but when implementing browserHistory I get a 404 error msg as I am trying to remove hashHistory in the url and as well would like to know a work around for production.
我整个下午都在拔头发,似乎看不出哪里出错了.
I been pulling out my hair all afternoon and can't seem to see where I am going wrong.
app/App.jsx
app/App.jsx
var React = require('react');
var ReactDom = require('react-dom');
var Router = require('react-router').Router;
var routes = require('./config/routes');
var browserHistory = require('react-router').browserHistory;
ReactDom.render(
<Router history={browserHistory}>{routes}</Router>,
document.getElementById('app')
);
app/config/routes.jsx
app/config/routes.jsx
var React = require('react');
var Main = require('../components/Main');
var Home = require('../components/Home');
var Profile = require('../components/Profile');
var Router = require('react-router');
var Route = Router.Route;
var IndexRoute = Router.IndexRoute;
module.exports = (
<Route path="/" component={Main}>
<Route path="profile/:username" component={Profile} />
<IndexRoute component={Home} />
</Route>
);
这是我正在处理的一个仓库https://github.com/SOSANA/React.JS-Tutorials-and-Things/tree/master/github-notetaker/04-state-and-props
Here is a repo that I am working on https://github.com/SOSANA/React.JS-Tutorials-and-Things/tree/master/github-notetaker/04-state-and-props
希望有人能帮忙
推荐答案
你需要在你的脚本标签中使用一个相对于根目录(或绝对)的 URL,否则应用程序最终会请求目录中的脚本并且服务器响应使用(包罗万象的)html 文件.
You need to use a root-relative (or absolute) URL in your script tag, otherwise the app ends up requesting the script in a directory and the server responds with the (catch-all) html file.
这篇关于react-router v2.0 browserHistory 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!