[英]How to get response from external js which is not in npm (node modules)
How to get response from external js which is not in npm (node modules). 如何从不在npm(节点模块)中的外部js获取响应。
Here i need to call some functions from react file and expecting the return value from external js file to be utilized in my component 在这里,我需要从react文件中调用一些函数,并期望在我的组件中使用外部js文件的返回值
Appending an external script on componentDidMount()
is an option. 可以在
componentDidMount()
上附加一个外部脚本。
componentDidMount() {
const script = document.createElement("script");
script.src = "path/to/your/file.js";
script.async = true;
script.onload = () => this.externalScriptLoaded();
document.body.appendChild(script);
}
Then inside externalScriptLoaded
, you can access functions in your external script using window
object. 然后在
externalScriptLoaded
内部,您可以使用window
对象访问外部脚本中的函数。
externalScriptLoaded() {
window.externaljs.someFunction(); // replace externaljs
}
You just need to make sure that the external module is loaded before you call any function from that module. 您只需要确保在调用外部模块中的任何函数之前已加载外部模块即可。 It is okay to do unless the external js manipulates the DOM.
除非外部js操纵DOM,否则可以这样做。
Edit: you can load the external js using <script>
tag 编辑:您可以使用
<script>
标签加载外部js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.