当前位置:网站首页>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')
);
}
边栏推荐
- if,case,for,while
- Learn iframes and use them to solve cross-domain problems
- 2022软件测试面试题 最新字节跳动50道真题面试题 刷完已拿下15k 附讲解+答疑
- 出现504怎么办?由于服务器更新导致的博客报504错误[详细记录]
- Reproduce 20-character short domain name bypass
- FFmpeg —— 录制麦克风声音(附源码)
- Jenkins export and import Job Pipeline
- For Qixi Festival, I made a confession envelope with code
- 内网服务器访问远程服务器的端口映射
- 系统设计.秒杀系统
猜你喜欢

8. Haproxy builds a web cluster

MySQL query optimization and tuning

pnpm 是凭什么对 npm 和 yarn 降维打击的

For Qixi Festival, I made a confession envelope with code

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

7-1 LVS+NAT 负载均衡群集,NAT模式部署

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

Functions, recursion and simple dom operations

Introduction to mq application scenarios

Implementing a server-side message active push solution based on SSE
随机推荐
Introduction to the memory model of the JVM
自定义通用分页标签01
JVM Notes
MRS: Introduction to the use of Alluxio
Postgresql源码(66)insert on conflict语法介绍与内核执行流程解析
Hey, I had another fight with HR in the small group!
Basic form validation process
A Preliminary Study of RSS Subscription to WeChat Official Account-feed43
Oracle与Postgresql在PLSQL内事务回滚的重大差异
Mockito unit testing
一文详解DHCP原理及配置
6-port full Gigabit Layer 2 network managed industrial Ethernet switch Gigabit 2 optical 4 electrical fiber self-healing ERPS ring network switch
mysql索引笔记
学会iframe并用其解决跨域问题
自定义通用分页标签02
Innovation and Integration | Huaqiu Empowerment Helps OpenHarmony Ecological Hardware Development and Landing
移动支付线上线下支付场景
FFmpeg —— 录制麦克风声音(附源码)
2 Gigabit Optical + 6 Gigabit Electric Rail Type Managed Industrial Ethernet Switch Supports X-Ring Redundant Ring One-key Ring Switch
八年软件测试工程师带你了解-测试岗进阶之路