如何配置tomcat 7与react-router-dom一起工作?

时间:2018-02-02 18:02:27

标签: reactjs tomcat react-router-dom

我有一个反应项目,它在开发版本中运行良好,但是当我导出它(bundle.js)并将它带到tomcat时,它无法识别react-router-dom的路由。

我需要在tomcat中配置一些东西吗?我找到了几个例子,但它们对我没用:

react routing is able to handle different url path but tomcat returns 404 not available resources

https://github.com/gildata/RAIO/issues/138

这是我的index.jsx:



import React from 'react';
import { render } from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { IntlProvider } from 'react-intl';
import { Provider } from 'react-redux';
import store from 'src/app/store';
import App from 'src/app/App';

render(
  <Provider store={store}>
    <IntlProvider locale="en">
      <BrowserRouter>
        <App />
      </BrowserRouter>
    </IntlProvider>
  </Provider>,
  document.getElementById('contenedor'),
);
&#13;
&#13;
&#13;

这是App.jsx

&#13;
&#13;
import React from 'react';
import { Switch, Route, Redirect } from 'react-router-dom';
import Login from 'src/app/Login';
import Menu from 'src/app/Menu';

const logueado = localStorage.getItem('tLogueado');

const App = () => (
  <main>
    <Switch>
      <Route exact path="/login" render={() => (logueado ? (<Redirect to="/onix" />) : (<Login />))} />
      <Route exact path="/onix" render={() => (logueado ? (<Menu />) : (<Redirect to="/login" />))} />
    </Switch>
  </main>
);

export default App;
&#13;
&#13;
&#13;

我做错了什么?请耐心等待我的反应。

1 个答案:

答案 0 :(得分:0)

有两种方法可以解决此问题

  1. 使用HashRouter代替BrowserRouter

  1. 使用基本名称,例如:<BrowserRouter basename="/calendar"/>

检查此链接how-to-deploy-a-react-app-to-a-subdirectory