如何在非节点应用程序中使用ReactRouter包含browserHistory?

时间:2016-07-26 11:11:39

标签: reactjs react-router

正在试验react并用browserHostory替换hashHistory。该应用程序是在apache服务器上运行而不在节点上运行的静态应用程序。已经将库用于React,Reaact-dom,ReactRouter并为各种路由创建了js页面。替换的代码似乎不起作用,并在控制台上收到消息Uncaught ReferenceError: browserHistory is not defined。下面是他的代码相同?我想用browserHistory替换哈希历史。

<script type="text/javascript">
            var destination = document.querySelector("#container");
            var { Router, Route,  IndexRoute, IndexLink, Link } = ReactRouter;

            ReactDOM.render(React.createElement(Router,{ history : browserHistory },React.createElement(
                Route,{ path: "/", component: App },
                React.createElement(IndexRoute,             { component: Home }),
                React.createElement(Route, { path: "/stuff",   component: Stuff }),
                React.createElement(Route, { path: "/contact", component: Contact }),
                React.createElement(Route, { path: "/event",   component: Event }),
                React.createElement(Route, { path: "/search",  component: Search }),
                React.createElement(Route, { path: "/fetch",   component: Fetch }),
                React.createElement(Route, { path: "/socket",  component: Socket }),
                React.createElement(Route, { path: "/form",    component: Form }) 
              )
            ), destination);

          </script>

如果使用node.js,则可以导入browserHistory,但是我在html页面中使用react-router库。如何克服这个问题? Image after discussion.

1 个答案:

答案 0 :(得分:0)

您也需要导入browserHistory

var { Router, Route,  IndexRoute, IndexLink, Link, browserHistory } = ReactRouter;