意外的'!'在 'worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker'
Unexpected '!' in 'worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker'
嗨,我正在尝试在 React 中使用 mapbox-gl 和 this tutorial。
我收到了这个错误:
意外的“!”在 'worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker'
在导入“worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker”
时
这是我的重要代码:
Map.js
import React from 'react'
import mapboxgl from 'mapbox-gl/dist/mapbox-gl-csp';
import MapboxWorker from 'worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker';
mapboxgl.workerClass = MapboxWorker;
mapboxgl.accessToken = " the token ";
export default class Map extends React.Component {
constructor(props) {
super(props);
this.state = {
lng: -70.9,
lat: 42.35,
zoom: 9
};
this.mapContainer = React.createRef();
}
componentDidMount() {
const { lng, lat, zoom } = this.state;
const map = new mapboxgl.Map({
container: this.mapContainer.current,
style: 'mapbox://styles/mapbox/streets-v11',
center: [lng, lat],
zoom: zoom
});
}
render() {
const style = {
"position": "absolute",
"top": "0",
"right": "0",
"left": "0",
"bottom": "0"
}
const { lng, lat, zoom } = this.state;
return (
<div>
<div ref={this.mapContainer} className={style} />
</div>
);
}
}
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import reportWebVitals from './reportWebVitals';
import Map from "./Map";
ReactDOM.render(
<Map/>,
document.getElementById('root')
);
reportWebVitals();
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<link href='https://api.mapbox.com/mapbox-gl-js/v2.1.1/mapbox-gl.css' rel='stylesheet' />
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
package.json
{
"name": "mapboxtest",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.5",
"@testing-library/user-event": "^12.8.1",
"mapbox-gl": "^2.1.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.0",
"worker-loader": "^3.0.7"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
谁能帮我解决这个问题?
这样做:
// eslint-disable-next-line import/no-webpack-loader-syntax
import MapboxWorker from 'worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker';
更多详情:
https://lifesaver.codes/answer/es-lint-rule-import-no-webpack-loader-syntax-please-deactivate
对于 TS 项目:
嗨,我正在尝试在 React 中使用 mapbox-gl 和 this tutorial。 我收到了这个错误: 意外的“!”在 'worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker' 在导入“worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker”
时这是我的重要代码:
Map.js
import React from 'react'
import mapboxgl from 'mapbox-gl/dist/mapbox-gl-csp';
import MapboxWorker from 'worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker';
mapboxgl.workerClass = MapboxWorker;
mapboxgl.accessToken = " the token ";
export default class Map extends React.Component {
constructor(props) {
super(props);
this.state = {
lng: -70.9,
lat: 42.35,
zoom: 9
};
this.mapContainer = React.createRef();
}
componentDidMount() {
const { lng, lat, zoom } = this.state;
const map = new mapboxgl.Map({
container: this.mapContainer.current,
style: 'mapbox://styles/mapbox/streets-v11',
center: [lng, lat],
zoom: zoom
});
}
render() {
const style = {
"position": "absolute",
"top": "0",
"right": "0",
"left": "0",
"bottom": "0"
}
const { lng, lat, zoom } = this.state;
return (
<div>
<div ref={this.mapContainer} className={style} />
</div>
);
}
}
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import reportWebVitals from './reportWebVitals';
import Map from "./Map";
ReactDOM.render(
<Map/>,
document.getElementById('root')
);
reportWebVitals();
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<link href='https://api.mapbox.com/mapbox-gl-js/v2.1.1/mapbox-gl.css' rel='stylesheet' />
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
package.json
{
"name": "mapboxtest",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.5",
"@testing-library/user-event": "^12.8.1",
"mapbox-gl": "^2.1.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.0",
"worker-loader": "^3.0.7"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
谁能帮我解决这个问题?
这样做:
// eslint-disable-next-line import/no-webpack-loader-syntax
import MapboxWorker from 'worker-loader!mapbox-gl/dist/mapbox-gl-csp-worker';
更多详情:
https://lifesaver.codes/answer/es-lint-rule-import-no-webpack-loader-syntax-please-deactivate
对于 TS 项目: