当前位置:网站首页>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')
);
}
边栏推荐
- "Introduction to nlp + actual combat: Chapter 8: Using Pytorch to realize handwritten digit recognition"
- 7-2 LVS+DR概述与部署
- 汇编语言之栈
- Deep learning -- CNN clothing image classification, for example, discussed how to evaluate neural network model
- 十一种概率分布
- Use serve to build a local server
- 2022年软件测试——精选金融银行面试真题
- 深度学习之 10 卷积神经网络3
- 烧录场景下开发如何进行源代码保密工作
- 使用serve搭建本地服务器
猜你喜欢
mq应用场景介绍
A Preliminary Study of RSS Subscription to WeChat Official Account-feed43
drools from download to postman request success
Significant differences between Oracle and Postgresql in PLSQL transaction rollback
将xml标签转换为txt(voc格式转换为yolo方便进行训练)
Eight guiding principles to help businesses achieve digital transformation success
目标检测-中篇
系统设计.如何设计一个秒杀系统(完整版 转)
2 Gigabit Optical + 6 Gigabit Electric Rail Type Managed Industrial Ethernet Switch Supports X-Ring Redundant Ring One-key Ring Switch
TL431的基本特性以及振荡电路
随机推荐
外卖店优先级
The general SQL injection flow (sample attached)
How class only static allocation and dynamic allocation
FPGA parsing B code----serial 3
文件系统的简单操作
系统设计.秒杀系统
centos 安装postgresql13 指定版本
2022年软件测试——精选金融银行面试真题
share总结
帮助企业实现数字化转型成功的八项指导原则
ADC噪声全面分析 -03- 利用噪声分析进行实际设计
【21天学习挑战赛】顺序查找
Y86. Chapter iv Prometheus giant monitoring system and the actual combat, Prometheus storage (17)
Oracle与Postgresql在PLSQL内事务回滚的重大差异
sql语句查询String类型字段小于10的怎么查
7-3 LVS+Keepalived Cluster Description and Deployment
Hey, I had another fight with HR in the small group!
Stop behind.
SVM介绍以及实战
7-2 LVS+DR概述与部署