当前位置:网站首页>How to dynamically add script dependent scripts
How to dynamically add script dependent scripts
2022-08-04 04:17:00 【act】
如何动态添加script依赖的脚本
使用的背景
在我们平常开发中,会在HTMLFile will be placed some rely on the script,但在有些情况下,Distinguish between different environment,While loading different script,Or the same script and different versions of,It's need to dynamically add
The concrete placement method
可以动态的创建script元素,The script then link tosrc,但要注意一点,After finish loading the script to the next step,Or placed script may not take effect or has been performed and rely on the script is still in loading.这是很重要的一点.
我所使用的框架是vit+react+ts进行开发的,So I added dynamicallyscriptElement is placed in theapp.tsx中.具体代码如下:例 Dynamic loading different versions of WeChatsdk版本
import React from 'react';
import ReactDOM from 'react-dom';
import {
BrowserRouter } from 'react-router-dom';
import {
getEnv } from '@/utils';//Distinguish between WeChat environment and enterprise WeChat environment
import App from './app';
// According to different environment insertjs-sdk,According to the current document enterprise WeChat insert1.2,WeChat insert1.6
const script = document.createElement('script');
// script.crossOrigin = 'anonymous';
if (getEnv() === 'qywx') {
script.src = 'https://res.wx.qq.com/open/js/jweixin-1.2.0.js';
script.onerror = () => {
window.$log('qy-wx-sdk:loadError');
};
} else {
script.src = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js';
script.onerror = () => {
window.$log('wx-sdk:loadError');
};
}
script.onload = rcInit;//加载完script后,渲染页面dom
document.head.appendChild(script);
function rcInit() {
ReactDOM.render(
<BrowserRouter basename="/admin-app">
<App />
</BrowserRouter>,
document.getElementById('root')
);
}
边栏推荐
- Postgresql source code (66) insert on conflict grammar introduction and kernel execution process analysis
- For Qixi Festival, I made a confession envelope with code
- Polygon zkEVM network node
- 数据集类型转换—TFRecords文件
- 软件测试如何系统规划学习呢?
- 7-1 LVS+NAT load balancing cluster, NAT mode deployment
- The Shell function
- 42. 接雨水
- FFmpeg —— 录制麦克风声音(附源码)
- 备份工具pg_dump的使用《postgres》
猜你喜欢

【21天学习挑战赛】图像的旋转问题(二维数组)

new Date converts strings into date formats Compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and explain the replace() method in detail

ADC噪声全面分析 -03- 利用噪声分析进行实际设计

自定义通用分页标签01
![[Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]](/img/f7/78eea9f14ca97b5e78592c7c2be313.png)
[Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]

Simple operation of the file system

Based on the statistical QDirStat Qt directory

劝退背后。

外卖店优先级

RSS订阅微信公众号初探-feed43
随机推荐
The general SQL injection flow (sample attached)
XSS related knowledge points
3000字,一文带你搞懂机器学习!
2022杭电多校联赛第五场 题解
出现504怎么办?由于服务器更新导致的博客报504错误[详细记录]
PL/SQL Some Advanced Fundamental
打造一份优雅的简历
数据治理平台项目总结和分析
【源码】使用深度学习训练一个游戏
How to automatically export or capture abnormal login ip and logs in elastic to the database?
劝退背后。
[Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]
FFmpeg —— 录制麦克风声音(附源码)
Learn iframes and use them to solve cross-domain problems
汇编语言之栈
【Ryerson情感说话/歌唱视听数据集(RAVDESS) 】
A Preliminary Study of RSS Subscription to WeChat Official Account-feed43
base address: environment variable
docker安装mysql与宿主机相差8小时的问题。
嵌入式数据库开发编程MySQL(全)