简体   繁体   English

如何从不在npm中的外部js获取响应(节点模块)

[英]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.

相关问题 Node.js:如何集中化npm模块? - Node.js : How to centralise npm modules ? Cordova:npm安装后如何从node_modules包含.js .css文件 - Cordova: How to include .js .css files from node_modules after npm install 如何从npm包中请求替代js文件而不指定完整的node_modules路径 - How to require an alternative js file from a npm package without specifiying full node_modules path 如何在 node.js 中导入模块或外部 .js 文件 - How to import modules or external .js files in node.js 如何在TypeScript中使用无类型的JS NPM模块用于节点? - How to use untyped JS NPM modules in TypeScript for node? 从调用 export.modules node.js 的 function 中获取变量 - Get variable out from function which is calling export.modules node.js 如何从 https 中获取数据。在 node.js 中获取响应 - How to get data from https.get response in node js 如何从node.js中获取请求的响应中获取数据? - How to get data from response to get request in node.js? 如何让我的ember.js应用程序导入从bower或npm安装的模块 - How do I get my ember.js app to import modules installed from bower or npm 如何使用ajax从javascript中的node.js获得响应? - how to get response from node.js in javascript by using ajax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM