react-router browserhistory 未能匹配任何路由

import React,{Component} from 'react';
import {render} from 'react-dom';
import { Router, Route, Link, browserHistory, IndexRoute, hashHistory} from 'react-router'

class App extends Component {

    constructor () {
        super();
        this.state = {};
    }

    render() {

        return (
            <div>
                {this.props.children}
            </div>
        )
    }

}

render((
    <Router history={browserHistory}>
        <Route path="/" component={App}>
            <IndexRoute component={Test} />
        </Route>
    </Router>
), document.getElementById('app'));

history 使用 browserHistory 就会有警告不能显示能容,使用 hashHistory 就正常,能显示内容

警告内容如下 :

browser.js:49Warning: [react-router] Location "/rjs/index.html#/" did not match any routes.

阅读 5.1k
2 个回答

因为hashHistory是根据url的hash部分(#)来改变,所以一般都会有个#,而browserHistory是通过调用浏览器history api来实现的,所以不用#。
推荐一下阮一峰的react-router教程:react-router入门

因为使用 hashHistory 改变的是 url 的哈希值,就是 # 号后面的内容,请求的文件是一样的。使用 browserHistory 改变了路径,请求的文件是不一样,所以 browserHistory 要通过服务器端设置来实现。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题