我在项目中的登陆页的componentDidMount中将window._vds设置成初始值,然后想把已经加载进来的脚本去掉,(之前登录进系统之后会有加载进来的js文件)然后重新加载,按理说,那个重新加载进来的js文件会将window._vds处理成一个其它的对象,但是我这段代码执行过后并没有使window._vds变成想要的对象结果。它还是一个初始定义的数组:
componentDidMount() {
// 给growingIO设置初始值
const script = document.getElementById('vds');
if (script) {
window._vds = [['setAccountId', 'xxx']];
script.parentNode.removeChild(script);
var vds = document.createElement('script');
vds.type = 'text/javascript';
vds.async = true;
vds.id = 'vds';
vds.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'dn-growing.qbox.me/vds.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(vds, s);
}
}
但是如果在控制台中手动执行一下下面代码中的内容:
window._vds = [['setAccountId', 'xxx']];
const script = document.getElementById('vds');
if (script) {
script.parentNode.removeChild(script);
var vds = document.createElement('script');
vds.type = 'text/javascript';
vds.async = true;
vds.id = 'vds';
vds.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'dn-growing.qbox.me/vds.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(vds, s);
}
这个window._vds就如愿变成了对象,可能是怎么回事呢?
只是script标签换了一下,没有真正请求那个js