react写熊掌号时需要将异步动态将创建的script写入文档,可是会有一个警告,导致我分享的时候并没有改变分享的标题等,请问怎么能解决 (注释的部分是我写的另外的方法,可是都没哟执行)
这行是我报的警告
Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened
下面试我写的代码
componentDidMount() {
let url = encodeURIComponent(location.href.split('#')[0]);
console.log(url)
const params = { url: url }
getjsssdk(params).then((res) => {
console.log(res)
if (res.data.code === 100) {
// var hm = document.createElement("script");
// hm.src = 'https://xiongzhang.baidu.com/sdk/c.js?' + res.data.data;
// var s = document.getElementsByTagName("script")[0];
// s.parentNode.insertBefore(hm, s);
this.script = document.createElement('script');
this.script.type = 'text/javascript';
this.script.src = 'https://xiongzhang.baidu.com/sdk/c.js?' + res.data.data;
// document.body.appendChild(this.script);
document.getElementsByTagName('body')[0].appendChild(this.script);
window.cambrian.share({
data: {
title: '熊掌号',
content: '熊掌号熊掌号熊掌号熊掌号熊掌号',
},
// 调起成功时的回调
success: function (data) {
},
// 调起失败时的回调
fail: function (data) {
}
});
}
}).catch((error) => {
});
}