OpenLayers 5 中 Cesium 集成的问题 - Cesium 未定义

Problem with Cesium integration in OpenLayers 5 - Cesium is not defined

一个关于 ol-Cesium 的快速问题。我正在尝试将 ol-Cesium 集成到我的 Vue - Openlayers 应用程序中。但是我遇到了这种类型的错误:

"ReferenceError: Cesium is not defined"

基本上我正在尝试的是在点击时激活 3d 功能,但我遇到了上面的错误。

我确实使用了示例中提供的代码

import OLCesium from 'olcs/OLCesium.js';

const ol3d = new OLCesium({map:  this.$store.getters.olMap}); 
ol3d.setEnabled(true);

我正在使用 OpenLayers v 5.3.0

错误表明这是一个 webpack 错误。

我引用的是此处找到的文档:https://github.com/gberaudo/ol-cesium-webpack-example/blob/master/webpack.config.js

确保你已经通过 NPM 安装了 Cesium:

npm i --save-dev cesium olcs copy-webpack-plugin

然后,在您的 webpack.config.js 文件中,添加以下行:

const cesiumSource = 'node_modules/cesium/Source';
const cesiumWorkers = '../Build/Cesium/Workers';
const CopywebpackPlugin = require('copy-webpack-plugin');

并在此文件的 configuration 对象中,添加以下行:

output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist'),

    // Needed to compile multiline strings in Cesium
    sourcePrefix: ''
},
amd: {
    // Enable webpack-friendly use of require in Cesium
    toUrlUndefined: true
},
node: {
    // Resolve node module use of fs
    fs: 'empty'
},

然后,将 Cesium 别名添加到此文件中:

resolve: {
    alias: {
        // CesiumJS module name
        cesium: path.resolve(__dirname, cesiumSource)
    }
},

然后,将其添加到此文件中的插件中:

 plugins: [
        new HtmlWebpackPlugin({
            template: 'src/index.html'
        }),
        // Copy Cesium Assets, Widgets, and Workers to a static directory
        new CopywebpackPlugin([ { from: path.join(cesiumSource, cesiumWorkers), to: 'Workers' } ]),
        new CopywebpackPlugin([ { from: path.join(cesiumSource, 'Assets'), to: 'Assets' } ]),
        new CopywebpackPlugin([ { from: path.join(cesiumSource, 'Widgets'), to: 'Widgets' } ]),
        new webpack.DefinePlugin({
            // Define relative base path in cesium for loading assets
            CESIUM_BASE_URL: JSON.stringify('')
        })
    ],

好的,我明白了。我只需要在指向 Cesium build

的 index.html 文件中添加脚本标签

示例如下:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width,initial-scale=1.0">
  <meta name="Vue-OpenLayers" content="Author: Agrivi d.o.o.; Wraping OpenLayers inside Vue.js">
  <link rel="icon" href="<%= BASE_URL %>agrivi.ico">
  <title>Agrivi Maps</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900">
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons">
  <script src="https://cesiumjs.org/releases/1.53/Build/Cesium/Cesium.js" charset="UTF-8"></script>
</head>

<body>
  <noscript>
    <strong>We're sorry but web-app doesn't work properly without JavaScript enabled. Please enable it to
      continue.</strong>
  </noscript>

  <div id="app"></div>
  <!-- built files will be auto injected -->
</body>

</html>

希望对大家有所帮助:)