无法读取未定义的属性(正在读取';OriginalPositionFor';)
我是反应的初学者,刚刚开始讨论反应-路由器-避孕套教程。
首先,我使用npm i react-router-dom
安装了REACTION-ROUTER-DOM
然后,在app.js中,我在函数头中使用默认代码,并在act-route-dom结构中调用此元素:
import logo from './logo.svg';
import './App.css';
import { BrowserRouter as Router,Routes,Route} from "react-router-dom";
function App() {
return (
<Router>
<Routes>
<Route path="/" element={<Header />} />
</Routes>
</Router>
);
}
export default App;
function Header(){
return(
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>Edit <code>src/App.js</code> and save to reload.</p>
<a className="App-link" href="https://reactjs.org" target="_blank" rel="noopener noreferrer">
Learn React
</a>
</header>
</div>
);
}
当我使用npm start
启动服务器时,收到以下错误:
log.js:59 Uncaught Error: Module build failed (from ./node_modules/babel-loader/lib/index.js):
TypeError: E:REACT TRAININGReactRouter
ode_moduleshistoryindex.js: Cannot read properties of undefined (reading 'originalPositionFor')
at SourceMapTree.originalPositionFor (E:REACT TRAININGReactRoutermy-app
ode_modules@ampprojectemappingdistemapping.umd.js:159:27)
at trace (E:REACT TRAININGReactRoutermy-app
ode_modules@ampprojectemappingdistemapping.umd.js:102:37)
at EncodedSourceMapImpl.map (E:REACT TRAININGReactRoutermy-app
ode_modules@jridgewell race-mappingdist race-mapping.umd.js:347:36)
at TraceMap.map (E:REACT TRAININGReactRoutermy-app
ode_modules@jridgewell race-mappingdist race-mapping.umd.js:430:31)
at SourceMapTree.traceMappings (E:REACT TRAININGReactRoutermy-app
ode_modules@ampprojectemappingdistemapping.umd.js:85:34)
at remapping (E:REACT TRAININGReactRoutermy-app
ode_modules@ampprojectemappingdistemapping.umd.js:258:36)
at mergeSourceMap (E:REACT TRAININGReactRoutermy-app
ode_modules@babelcorelib ransformationfilemerge-map.js:19:30)
at generateCode (E:REACT TRAININGReactRoutermy-app
ode_modules@babelcorelib ransformationfilegenerate.js:72:39)
at run (E:REACT TRAININGReactRoutermy-app
ode_modules@babelcorelib ransformationindex.js:55:33)
at run.next (<anonymous>)
at Object.../node_modules/history/index.js (http://localhost:3000/static/js/bundle.js:41373:7)
at Object.options.factory (http://localhost:3000/static/js/bundle.js:46449:31)
at __webpack_require__ (http://localhost:3000/static/js/bundle.js:45899:33)
at fn (http://localhost:3000/static/js/bundle.js:46120:21)
at Module.../node_modules/react-router-dom/index.js (http://localhost:3000/static/js/bundle.js:41527:65)
at Module.options.factory (http://localhost:3000/static/js/bundle.js:46449:31)
at __webpack_require__ (http://localhost:3000/static/js/bundle.js:45899:33)
at fn (http://localhost:3000/static/js/bundle.js:46120:21)
at Module../src/App.js (http://localhost:3000/static/js/bundle.js:68:74)
at Module.options.factory (http://localhost:3000/static/js/bundle.js:46449:31)
我尝试查找各种来源,但找不到任何解决方案。
依赖项:dependencies": { "@testing-library/jest-dom": "^5.16.2", "@testing-library/react": "^12.1.2", "@testing-library/user-event": "^13.5.0", "react": "^17.0.2", "react-dom": "^17.0.2", "react-scripts": "5.0.0", "web-vitals": "^2.1.4" }
解决方案
将@babel/core降级到以前的版本,已为我修复了该问题
npm i -D @babel/core@7.16.12
相关文章