使用react-router可以访问完整路径并重新加载页面

时间:2016-11-11 15:43:51

标签: reactjs routing react-router

我在反应项目中使用react-router。 这是我的package.json:

{
  "name": "app_name",
  "version": "1.0.0",
  "description": "app on browser",
  "main": "gulpfile.js",
  "scripts": {
    "build": "gulp build",
    "start": "cordova run browser"
  },
  "author": "maki",
  "license": "ISC",
  "dependencies": {
    "babel-preset-es2015": "^6.13.2",
    "browserify": "^13.1.0",
    "gulp": "^3.9.1",
    "history": "^4.2.0",
    "material-design-icons": "^2.2.3",
    "material-ui": "^0.15.4",
    "react": "^15.3.0",
    "react-dom": "^15.3.1",
    "react-motion": "^0.4.4",
    "react-router": "^2.7.0",
    "react-swipeable-views": "^0.7.0",
    "react-tap-event-plugin": "^1.0.0",
    "vinyl-source-stream": "^1.1.0"
  },
  "devDependencies": {
    "babel-cli": "^6.11.4",
    "babel-plugin-transform-class-properties": "^6.11.5",
    "babel-preset-react": "^6.11.1",
    "babel-preset-stage-1": "^6.13.0",
    "babelify": "^7.3.0",
    "gulp-sass": "^2.3.2",
    "jquery": "^3.1.0"
  }
}

我有一个如下路由器:

<Router history={browserHistory}>
  <Route path="/" component={App}>
    <IndexRoute component={HomePage}/>
    <Route path="/news" component={NewsPage}/>
      <Route path="/news/:id" components={News}/>
    <Route path="/about" component={AboutPage}/>
    <Route path="/member" component={MemberPage}/>
    <Route path="/products" component={ProductsPage}/>
    <Route path="/contact" component={ContactPage}/>
  </Route>
</Router>

这是我想要实现的目标:
1:访问除IndexRoute之外。
ex)http://example.com/news/1234
2:提供除IndexRoute页面之外的重新加载。

现在,我尝试了,我收到了这条消息:

  

无法获取/新闻

我该怎么办?

1 个答案:

答案 0 :(得分:1)

您需要在gulpfile中实现browserify的历史回退,类似于:

var historyApiFallback = require('connect-history-api-fallback');

// ...
browserSync({
  server: {
    baseDir: './',
    middleware: [historyApiFallback()],
  },
});