当前位置:网站首页>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')
);
}
边栏推荐
- 八年软件测试工程师带你了解-测试岗进阶之路
- manipulation of file contents
- 转:管理是对可能性的热爱,管理者要有闯进未知的勇气
- Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch
- SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropri
- [Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this
- 8. Haproxy builds a web cluster
- SVM介绍以及实战
- 马尔可夫链
- 8.Haproxy 搭建Web集群
猜你喜欢

Functions, recursion and simple dom operations

A Preliminary Study of RSS Subscription to WeChat Official Account-feed43

4-way two-way HDMI integrated business high-definition video optical transceiver 8-way HDMI high-definition video optical transceiver

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

Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch

drools从下载到postman请求成功
![[Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this](/img/d0/6ac51d0d51c907ed0e1578e038fffd.jpg)
[Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this

2022杭电多校联赛第五场 题解

SQL query String field less than 10 how to check

复现20字符短域名绕过
随机推荐
MRS: Introduction to the use of Alluxio
结构体函数练习
FFmpeg —— 录制麦克风声音(附源码)
docker+bridge+redis master-slave+sentry mode
【MD5】采用MD5+盐的加密方式完成注册用户和登录账号
结构体指针知识要点总结
数据治理平台项目总结和分析
移动支付线上线下支付场景
[Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this
XSS相关知识点
7-2 LVS+DR Overview and Deployment
2022年软件测试——精选金融银行面试真题
Embedded database development programming MySQL (full)
缓存穿透、缓存击穿、缓存雪崩以及解决方案
7.LVS负载均衡群集之原理叙述
用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
"Introduction to nlp + actual combat: Chapter 8: Using Pytorch to realize handwritten digit recognition"
SQL query String field less than 10 how to check
Innovation and Integration | Huaqiu Empowerment Helps OpenHarmony Ecological Hardware Development and Landing
基于 SSE 实现服务端消息主动推送解决方案