From df924bf10a227589397cfd81f714c8bbd62dca44 Mon Sep 17 00:00:00 2001 From: LiuJiaNan <15703339975@163.com> Date: Mon, 1 Dec 2025 11:12:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EdynamicLoadJs=E5=92=8Cdynamic?= =?UTF-8?q?LoadCss=EF=BC=8C=E4=BC=98=E5=8C=96Map?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/Map/MapSelector.js | 3 +++ components/Map/index.js | 13 ++++++++----- utils/index.d.ts | 10 ++++++++++ utils/index.js | 29 +++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/components/Map/MapSelector.js b/components/Map/MapSelector.js index d228c02..aeace24 100644 --- a/components/Map/MapSelector.js +++ b/components/Map/MapSelector.js @@ -1,5 +1,6 @@ import { Button, Col, Form, Input, Modal, Row, Select, Spin } from "antd"; import { useEffect, useRef, useState } from "react"; +import { dynamicLoadJs } from "../../utils"; /** * 定位组件弹窗 @@ -33,6 +34,8 @@ const MapSelector = (props) => { // 初始化地图 const initMap = async () => { + await dynamicLoadJs("https://api.map.baidu.com/api?v=1.0&type=webgl&ak=OElqFYoKiAH8KFtph8ftLKF5NlNrbCUr"); + if (!window.BMapGL) { console.error("BMapGL is not loaded"); return; diff --git a/components/Map/index.js b/components/Map/index.js index 04c930f..27c4990 100644 --- a/components/Map/index.js +++ b/components/Map/index.js @@ -45,11 +45,14 @@ const Map = (props) => { - diff --git a/utils/index.d.ts b/utils/index.d.ts index ab90594..940309c 100644 --- a/utils/index.d.ts +++ b/utils/index.d.ts @@ -332,3 +332,13 @@ export function processTreeDataForOnlyLastLevel( export function validatorEndTime(timeStart: string): { validator: (_: any, value: any) => Promise; }; + +/** + * 动态加载js资源 + */ +export function dynamicLoadJs(url: string): Promise; + +/** + * 动态加载css资源 + */ +export function dynamicLoadCss(url: string): Promise; diff --git a/utils/index.js b/utils/index.js index c78045e..73da194 100644 --- a/utils/index.js +++ b/utils/index.js @@ -523,6 +523,35 @@ export const validatorEndTime = (timeStart) => { } } +/** + * 动态加载js资源 + */ +export function dynamicLoadJs(url) { + return new Promise((resolve, reject) => { + const script = document.createElement("script"); + script.type = "text/javascript"; + script.src = url; + script.onload = resolve; + script.onerror = reject; + document.body.appendChild(script); + }); +} + +/** + * 动态加载css资源 + */ +export function dynamicLoadCss(url) { + return new Promise((resolve, reject) => { + const link = document.createElement("link"); + link.rel = "stylesheet"; + link.type = "text/css"; + link.href = url; + link.onload = resolve; + link.onerror = reject; + document.head.appendChild(link); + }); +} + /** * 获取文件url */