首页 前端知识 vue2中引入cesium

vue2中引入cesium

2024-08-05 23:08:25 前端知识 前端哥 268 168 我要收藏

效果图:

 哎呀,刚做前端就遇到了一个知识盲点,如何在vue2中引入cesium?这几天也是煞费苦心!废话不多说,直接上代码!

1.创建vue2项目

2.安装cesium

npm install cesium@1.95.0 -s

 3.安装webpack

npm install copy-webpack-plugin@10.2.4

 4.安装webpack所需loader

npm install @open-wc/webpack-import-meta-loader

5.配置vue.config.js

const {defineConfig} = require('@vue/cli-service')
const CopyWebpackPlugin = require("copy-webpack-plugin");
const webpack = require("webpack");
const path = require("path");
 
let cesiumSource = "node_modules/cesium/Source";
let cesiumWorkers = "Workers";
module.exports = defineConfig({
  transpileDependencies: true,
  lintOnSave: false,
  configureWebpack: {
    externals: {
      'cesium': 'Cesium',
    },
    output: {
      sourcePrefix: " ", 
    },
    amd: {
      toUrlUndefined: true, 
    },
    resolve: {
      extensions: ['.js', '.vue', '.json'],
      alias: {
        "@": path.resolve("src"),
        cesium: path.resolve(__dirname, cesiumSource),
      }
    },
    plugins: [
      new CopyWebpackPlugin({
        patterns: [
          {from: path.join(cesiumSource, cesiumWorkers), to: "Workers"},
          {from: path.join(cesiumSource, "Assets"), to: "Assets"},
          {from: path.join(cesiumSource, "Widgets"), to: "Widgets"},
          {from: path.join(cesiumSource, "ThirdParty/Workers"), to: "ThirdParty/Workers"}
        ]
      }),
      new webpack.DefinePlugin({
        CESIUM_BASE_URL: JSON.stringify("./"),
      }),
    ],
    module: {
      unknownContextCritical: false,
      unknownContextRegExp: /\/cesium\/cesium\/Source\/Core\/buildModuleUrl\.js/,
      rules: [
        {
          test: /\.js$/,
          use: {
            loader: '@open-wc/webpack-import-meta-loader',
          },
        },
      ]
    },
  },
  devServer: {
    hot: true,
    open: true,
    // 代理
    proxy: {
      '/api': {
        target: 'http://localhost:8888/',
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    },
    port: 8080,
  }
})

6.main.js中导入cesium的css

import 'cesium/Widgets/widgets.css';

 7App.vue 配置style

 

html,body,#cesiumContainer {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;

9.npm run serve!!

 

转载请注明出处或者链接地址:https://www.qianduange.cn//article/14844.html
标签
评论
发布的文章

JQuery的选择器有哪些?

2024-08-14 22:08:43

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!