React-redux 找不到商店

React-redux could not find store

我正在使用 React + Redux + Express。每当我重新加载我的 localhost/courses 时,我都会收到找不到 'store' 的错误。但如果我从 localhost -> click to nav course 开始,一切正常。我该如何解决?

错误

Invariant Violation: Could not find "store" in either the context or props of "Connect(Course)". Either wrap the root component in a Provider, or explicitly pass "store" as a prop to "Connect(Course)".

store.js

const middleware = applyMiddleware(promise(), thunk, logger());

export default createStore(reducer, middleware);

App.js

window.onload = () => {
  ReactDOM.render(
    <Provider store={store}>
      <Router history={browserHistory} routes={routes} onUpdate={() => window.scrollTo(0, 0)}/>
    </Provider>
    , document.getElementById('main'));
};

routes.js

const routes = (
  <Route path='/' component={Layout}>
    <IndexRoute component={Home}/>
    <Route path='courses' data={data} component={Course}>          
  </Route>
);

export default routes;

Course.js

class Course extends React.Component {
  static contextTypes = {
    router: React.PropTypes.object,
  };

  componentWillMount() {
    this.props.dispatch(fetchVocabulary());
  };

  render() {
    console.log(this.props.words);
    return (
      <div>
        ...
      </div>
    );
  };
}

export default connect((store) => {
  return {
    words: store.vocabulary.words
  };
})(Course);

package.json

"react": "^15.4.1",
"react-dom": "^15.4.1",
"react-hot-loader": "^1.3.1",
"react-redux": "^5.0.1",
"react-router": "^3.0.0",
"redux": "^3.6.0",
"redux-logger": "^2.7.4",
"redux-promise-middleware": "^4.2.0",
"redux-thunk": "^2.1.0",

由于您正在使用 browserHistory ,因此您需要使用 historyApiFallback

devServer: {
        historyApiFallback:true
    }

如果路由不匹配任何其他文件,则使用 connect-history-api-fallback 提供服务 index.html,然后所有路由都与此文件相关