我在React Next上使用openlayers库。通过npm添加openlayers库。但是,当我导入“从'ol / Map'中导入地图”库时,出现以下错误。
import PluggableMap from './PluggableMap.js';
^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at new Script (vm.js:80:7)
at createScript (vm.js:274:10)
at Object.runInThisContext (vm.js:326:10)
at Module._compile (internal/modules/cjs/loader.js:664:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.ol/Map.js at Object.ol/Map.js (<path>\.next\server\static\development\pages\index.js:1730:18)
at __webpack_require__ (<path>\.next\server\static\development\pages\index.js:23:31)
at Module../components/mappanel/map.js (<path>\.next\server\static\development\pages\index.js:557:67)
at __webpack_require__ (<path>\.next\server\static\development\pages\index.js:23:31)
at Module../pages/index.js (<path>\.next\server\static\development\pages\index.js:1302:83)
at __webpack_require__ <path>\.next\server\static\development\pages\index.js:23:31)
在next.config.js和.babelrc文件中我什么也没做。如何解决该错误?
注意:Openlayers与经典的React一起工作。
沙箱链接在这里:https://codesandbox.io/s/react-openlayers-7r04v
谢谢
答案 0 :(得分:0)
似乎ol
在服务器端不起作用,因为它需要窗口之类的东西,因此您需要这样做
componentDidMount() {
const map = require("ol/Map").default;
}