gpt4 book ai didi

javascript - 读取javascript脚本index.html公共(public)文件夹中的类react js

转载 作者:行者123 更新时间:2023-11-28 03:11:35 25 4
gpt4 key购买 nike

我需要知道如何将index.html公共(public)文件夹文件中的javscript脚本嵌入到react组件中。我已经使用了这个方法,但类仍然未定义。

componentDidMount () {
this._loadScript('/library/es6-shim.js', false)
this._loadScript('/library/fingerprint.sdk.min.js', true)
this._loadScript('/library/websdk.client.bundle.min.js', false)
var script = document.createElement('script')
script.async = true
script.innerHTML = "this.sdk = new Fingerprint.WebApi;"
document.head.appendChild(script)
}

_loadScript = (src, type) => {
var tag = document.createElement('script');
tag.async = true;
tag.src = src;
document.head.appendChild(tag)
// tag.onload = () => this.scriptLoaded()

console.log('headers', document.head)
}

我需要在我的组件中读取新的 Fingerprint.WebApi 实例。

最佳答案

您应该加载所有三个库,然后您应该可以访问全局公开的 Fingerprint 类。

componentDidMount() {
Promise.all([
this._loadScript('/library/es6-shim.js'),
this._loadScript('/library/fingerprint.sdk.min.js'),
this._loadScript('/library/websdk.client.bundle.min.js')
])).then(() => {
// Fingerprint should be available on the window.
console.log(window.Fingerprint);
const sdk = new window.Fingerprint.WebApi();
});
}
_loadScript = (src) => {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('async', 'true');
script.setAttribute('src', url);
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}

关于javascript - 读取javascript脚本index.html公共(public)文件夹中的类react js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60054309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com