开发者问题收集

React Router 无法找到历史记录

2020-09-15
1904

我的 React 项目中有这个 index.js :

import React from 'react';
import ReactDOM from 'react-dom';
import './css/main.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import {BrowserRouter as Router} from 'react-router-dom';

ReactDOM.render(
  <React.StrictMode>
    <Router>
      <App />
    </Router>
  </React.StrictMode>,
  document.getElementById('root')
);

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: 
serviceWorker.unregister();

这是我的 app.js :

import React from 'react';
import {Routes, Route} from 'react-router';

import Catalogs from './components/pages/Catalogs';

function App() {
  return (
    <div>
      <Routes>
        <Route path="/" element={<Catalogs />} />
      </Routes>
    </div>
  );
}

export default App;

但是我在 npm start 时收到此错误: ./node_modules/react-router/index.js 未找到模块:无法解析“/Users/hugovillalobos/Documents/Code/cinetogoproject/frontend/cinetogo/node_modules/react-router”中的“history”

我不知道我遗漏了什么。

2个回答

文档对此只字未提,但就我而言,通过安装 history 解决了该问题: npm install --save history

HuLu ViCa
2020-09-15

问题可能在于在 app.js 中从 react-router 而不是 react-router-dom 导入

Mike Kokadii
2020-09-16