当前位置:网站首页>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源码(66)insert on conflict语法介绍与内核执行流程解析

看DevExpress丰富图表样式,如何为基金公司业务创新赋能

七夕节,我用代码制作了表白信封

Postgresql source code (66) insert on conflict grammar introduction and kernel execution process analysis

Stop behind.

拿捏JVM性能优化(自己笔记版本)

深度学习之 10 卷积神经网络3

How to systematically plan and learn software testing?

Simple operation of the file system

文件系统的简单操作
随机推荐
结构体指针知识要点总结
烧录场景下开发如何进行源代码保密工作
SQL injection in #, - +, - % 20, % 23 is what mean?
使用serve搭建本地服务器
Mobile payment online and offline payment scenarios
【 observe 】 super fusion: the first mention of "calculate net nine order" evaluation model, build open prosperity of power network
类如何只能静态分配和只能动态分配
张量篇-应用案例
Enterprise live broadcast is on the rise: Witnessing focused products, micro-like embracing ecology
Hey, I had another fight with HR in the small group!
Metaverse "Drummer" Unity: Crazy expansion, suspense still exists
7-1 LVS+NAT load balancing cluster, NAT mode deployment
机器学习模型的“可解释性”
Postgresql源码(66)insert on conflict语法介绍与内核执行流程解析
The general SQL injection flow (sample attached)
Simple operation of the file system
[Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]
docker+bridge+redis master-slave+sentry mode
7-2 LVS+DR Overview and Deployment
移动支付线上线下支付场景