【发布时间】:2016-08-19 19:44:18
【问题描述】:
在页面http://localhost/about 上刷新时未找到以下代码报告 404。但是如果把browserHistory改成hashHistory就可以了。
这是我的 js 文件。
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, IndexRoute, Link, IndexLink, browserHistory, hashHistory } from 'react-router';
import $ from 'jquery';
class App extends Component {
render() {
return (
<div>
<h1>APP!</h1>
<Link to="/about">/about</Link>
{this.props.children}
</div>
)
}
}
class About extends React.Component {
render() {
return (
<div>
<h2>About 33</h2>
</div>
)
}
}
var routes = (
<Router history={hashHistory}>
<Route path="/" component={App} />
<Route path="/about" component={About} />
<Route path="/wealth" component={WealthExpectation} />
</Router>
)
$(document).ready(function() {ReactDOM.render(routes, document.getElementById("hello"))});
还有html文件。
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello React</title>
<script type="text/javascript" src="/static/js/jquery-1.12.2.min.js"></script>
<script type="text/javascript" src="/static/js/script.js"></script>
<!-- build:css -->
<link rel="stylesheet" type="text/css" href="/static/bower_modules/c3/c3.min.css">
<!-- endbuild -->
</head>
<body>
<div id="hello">a</div>
<div id="world"></div>
</body>
</html>
我已阅读有关react-router v2.0 browserHistory not working 和React-router urls don't work when refreshing or writting manually 的问题。对于第一个,我已经将路径设置为绝对路径,但仍然无法正常工作。对于第二个,我尝试导入 express 但失败('Uncaught TypeError: Cannot read property 'prototype' of undefined')。
【问题讨论】:
-
你用的是快递吗?
-
我尝试导入 express 4,但报 'Uncaught TypeError: Cannot read property 'prototype' of undefined'
-
我不明白,你用快递处理你的路线吗?
-
不,不是现在。前几天试了,没成功,把相关代码删了。
-
使用浏览器历史记录你需要在你的后端实现 /about,你可以使用 express 来做到这一点: var express = require('express'); var app = express(); // 当对主页发出 GET 请求时响应“hello world” app.get('/', function(req, res) { res.render('index.html'); }); app.get('/about', function(req, res) { res.render('index.html'); });
标签: reactjs react-router jsx