关于javascript:arcgis的loadModules与proxy的初次结合

每次调用esri-loader的包都须要去引入,就很麻烦

偶尔发现的新鲜写法

import { loadModules } from "esri-loader";

// 配置arcgis中各个包与绝对应的门路
const __arcgisUrl = {
  MapView: "esri/views/MapView",
  Map: "esri/Map",
};

// 不暴漏在外的存储对象
const __arcgis = {};

// 定义裸露的arcgis变量
const arcgisPackage = new Proxy(
  {},
  {
    // 拦挡取值
    async get(target, key) {
      // console.log(key, receiver);
      // console.log(config);
      let val = {};
      if (typeof __arcgis[key] === "undefined") {
        [val] = await loadModules([__arcgisUrl[key]], {
          url: "http://***.***.***.***:8686/415/init.js",
          dojoConfig: {
            baseUrl: "http://***.***.***.***:8686/415/dojo",
          },
        });
        __arcgis[key] = val;
      } else {
        val = __arcgis[key];
      }

      return val;
    },
    set() {
      console.warn("arcgisPackage对象属性不能赋值");
    },
  }
);

export default arcgisPackage;

原作者👇👇👇
https://segmentfault.com/u/yo…
vue组件调用

import arcgisPackage from "@/config/arcgisTools";
methods:{
   test(){
     let MapView = await arcgisPackage.MapView;
   }
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理