当前位置:网站首页>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')
);
}
边栏推荐
- Significant differences between Oracle and Postgresql in PLSQL transaction rollback
- "Introduction to nlp + actual combat: Chapter 8: Using Pytorch to realize handwritten digit recognition"
- 【id类型和NSObject指针 ObjectIve-C中】
- typescript type 和 interface 的区别
- Take care of JVM performance optimization (own note version)
- 马尔可夫链
- 【C语言进阶】程序环境和预处理
- mysql索引笔记
- Functions, recursion and simple dom operations
- Embedded database development programming MySQL (full)
猜你喜欢

Eight guiding principles to help businesses achieve digital transformation success

Explain detailed explanation and practice

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

结构体指针知识要点总结

10 Convolutional Neural Networks for Deep Learning 3

7-2 LVS+DR Overview and Deployment

帮助企业实现数字化转型成功的八项指导原则

小程序 + 电商,玩转新零售

SQL query String field less than 10 how to check

2003. 每棵子树内缺失的最小基因值 DFS
随机推荐
Based on the statistical QDirStat Qt directory
Mobile payment online and offline payment scenarios
网络工程师入门必懂华为认证体系,附系统学习路线分享
【Ryerson情感说话/歌唱视听数据集(RAVDESS) 】
mq应用场景介绍
docker安装mysql与宿主机相差8小时的问题。
Introduction to the memory model of the JVM
移动支付线上线下支付场景
用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
系统设计.如何设计一个秒杀系统(完整版 转)
Jenkins 导出、导入 Job Pipeline
Shell 函数
MRS: Introduction to the use of Alluxio
张量篇-应用案例
马尔可夫链
SQL interview Questions
pnpm 是凭什么对 npm 和 yarn 降维打击的
unity框架之缓存池
TL431的基本特性以及振荡电路
A Preliminary Study of RSS Subscription to WeChat Official Account-feed43