我正在尝试将openlayers与typescript一起使用,但是有问题。
openlayers用于MapSection组件,下面是MapSection代码
import React, { useLayoutEffect } from 'react'
import * as ol from 'openlayers';
const MapSection: React.FC = () => {
useLayoutEffect(() => {
const map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM(),
})],
target: 'map-section',
view: new ol.View({
center: [36, 38],
zoom: 16,
}),
controls: [],
});
map.getKeys();
}, [])
return (
<section className='map-section'>
</section>
)
}
export default MapSection;
如果我运行webpack devServer,则会显示错误消息
ERROR in ./src/components/MapSection.tsx Module not found: Error: Can't resolve 'openlayers' in '/Users/donghokim/recruit/angelswing-frontend-test-0.3/src/components' @ ./src/components/MapSection.tsx 2:0-33 6:18-24 7:19-27 8:20-29 11:16-23 @ ./src/components/App.tsx @ ./src/index.tsx
奇怪的是,该组件试图在当前目录中找到openlayers库。
webpack和tsconfig文件是这样写的
//webpack.config.js
import path from "path";
import HTMLWebpackPlugin from 'html-webpack-plugin';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import webpack from "webpack";
const config: webpack.Configuration = {
entry: path.join(__dirname, 'src', 'index.tsx'),
context: path.resolve(__dirname, '.'),
devtool: 'source-map',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, './build'),
publicPath: '/'
},
resolve: {
extensions: ['.ts', '.tsx', '.js', 'json'],
modules: [
'node_modules',
path.resolve(__dirname, './node_modules')
],
},
module: {
rules: [
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript"
]
}
},
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
]
},
plugins: [
new HTMLWebpackPlugin({
template: './public/index.html'
}),
new ForkTsCheckerWebpackPlugin({
async: false,
eslint: {
files: "./src/**/*",
},
}),
]
}
export default config;
//tsconfig.json
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"outDir": "build",
"module": "CommonJS",
"target": "ES5",
"jsx": "react",
"moduleResolution": "node",
"experimentalDecorators": true,
"skipDefaultLibCheck": true,
"sourceMap": true,
"allowJs": true,
"strict": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"]
},
"include": ["src"],
"exclude": ["build/**/*"]
}
我只想知道在哪里可以找到开始寻找问题的机会。
///添加了package.json设置
...
"dependencies": {
"ol": "^6.4.3",
"react": "^16.13.1",
"react-dom": "^16.13.1"
},
"devDependencies": {
"@babel/core": "^7.11.6",
"@babel/plugin-transform-runtime": "^7.12.1",
"@babel/preset-env": "^7.11.5",
"@babel/preset-react": "^7.10.4",
"@babel/preset-typescript": "^7.12.1",
"@babel/runtime": "^7.12.1",
"@types/fork-ts-checker-webpack-plugin": "^0.4.5",
"@types/openlayers": "^4.6.17",
"@types/react": "^16.9.55",
"@types/react-dom": "^16.9.9",
"@types/styled-components": "^5.1.4",
"@types/webpack": "^4.41.24",
"@types/webpack-dev-server": "^3.11.0",
"@typescript-eslint/eslint-plugin": "^4.6.0",
"@typescript-eslint/parser": "^4.6.0",
"babel-loader": "^8.1.0",
"css-loader": "^4.3.0",
"eslint": "^7.12.1",
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-react-hooks": "^4.2.0",
"fork-ts-checker-webpack-plugin": "^5.2.1",
"html-webpack-plugin": "^4.5.0",
"style-loader": "^1.3.0",
"ts-node": "^9.0.0",
"typescript": "^4.0.5",
"webpack": "^4.44.2",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0"
}