react-router位置与任何路由都不匹配

时间:2016-01-17 10:06:27

标签: javascript reactjs react-router

我被反应路由器路由困住了。我收到了错误:

Warning: [react-router] Location "/FluxApp/" did not match any routes

这是我的 app.js

var React = require('react');
var ReactDOM = require('react-dom');
var Router = require('react-router').Router;
var Route = require('react-router').Route;
var IndexRoute = require('react-router').IndexRoute;
var browserHistory = require('react-router').browserHistory;

var App = require('./views/App');
var Home = require('./views/Home');


var Routes = (<Router history={browserHistory}>
               <Route path="/" component={App}>
                 <IndexRoute component={Home} />
               </Route>
              </Router>);

ReactDOM.render(Routes, document.getElementById('content'));

我的 App.js 如下所示:

var React = require('react');
var RouteHandler = require('react-router').RouteHandler;

var App = React.createClass({
  render: function() {
     return (
        <RouteHandler />
     );
  }
});

module.exports = App;

我的 Home.js 如下所示:

var React = require('react');

var Home = React.createClass({

  render: function() {
    return (
      <div>
        Home
      </div>
    );
  }

});

module.exports = Home;

这是我的项目的hieararchy:

/FluxApp
   |...
   +/js
   .  |
   .  +/actions
   .  +/constants
   .  +/dispatcher
   .  +/stores
   .  +/views
   .  .     |
   .  .     App.js
   .  .     Home.js
   .  app.js
   .
   index.html

正如您所猜测,我使用browserify构建 app.js 并创建 bundle.js ,我在脚本中使用 bundle.js index.html

中的标记

以下是我在项目中使用的所有版本。

"dependencies": {
   "classnames": "^2.2.3",
   "flux": "^2.1.1",
   "keymirror": "^0.1.1",
   "object-assign": "^1.0.0",
   "react": "^0.14.6",
   "react-dom": "^0.14.6",
   "react-router": "^2.0.0-rc5"
},
"devDependencies": {
   "browserify": "^6.2.0",
   "envify": "^3.0.0",
   "jest-cli": "^0.4.3",
   "reactify": "^0.15.2",
   "uglify-js": "~2.4.15",
   "watchify": "^2.1.1"
},

所以,当我尝试转到“http://localhost:8080/FluxApp/”时,我总是得到同样的错误:“警告:[react-router]位置”/ FluxApp /“与任何路线都不匹配”

我该如何解决这个问题? 感谢。

2 个答案:

答案 0 :(得分:7)

您的路线实际上并未指定& > < / "' ; : ) = ( ] [ # } { \的路线。

你需要这样的东西:

app.js

中的

/FluxApp
App.js

var Routes = (<Router history={browserHistory}>
               <Route path="/FluxApp/" component={App}>
                <IndexRoute component={Home} />
               </Route>
              </Router>);

答案 1 :(得分:0)

使用webpack实现React Router v4,如下所示

import { BrowserRouter as Router, Route } from 'react-router-dom';

ReactDOM.render(
  <Router>
    <div>
      <Route exact path="/" render={() => <h1>main page</h1>} />
      <Route path="/about" render={() => <h1>about page</h1>} />
      <Route path="/contact" render={() => <h1>contact page</h1>} />
    </div>
  </Router>,
  document.getElementById('root'));
  
  //webpack  build  script change add --history-api-fallback in package.json file
  "scripts": {
    "start": "webpack-dev-server --history-api-fallback"
    }

现在在终端上运行以下命令:npm start