[英]Importing Javascript in React TypeScript component
I'm starting a new React project in TypeScript and its a conversion of an old project that was done in basic JS and HTML.我正在 TypeScript 中开始一个新的 React 项目,它是在基本 JS 和 HTML 中完成的旧项目的转换。 There is a web library written in JS I'm trying to use but I'm not sure how to import it.
我正在尝试使用用 JS 编写的 web 库,但我不确定如何导入它。 In the old project it was just imported with a script tag in head.
在旧项目中,它只是在 head 中使用脚本标记导入的。 The old function called in the embedded JS was
let external = $pop.render(params)
.在嵌入式 JS 中调用的旧 function 被
let external = $pop.render(params)
。 The extension for the old import in the script tag was /index.js?mode=api
.脚本标记中旧导入的扩展名是
/index.js?mode=api
。 Any help would be appreciated.任何帮助,将不胜感激。 Thanks in advance!
提前致谢! The library is a custom private js library.
该库是一个自定义的私有 js 库。 I have tried
import "URL TO LIBRARY";
我试过
import "URL TO LIBRARY";
, jQuery.getScript()
, and import * as Pop from 'URL';
,
jQuery.getScript()
, 并import * as Pop from 'URL';
. . It is an AMD based module.
它是一个基于 AMD 的模块。
I was able to solve it by keeping it as a script tag import in the HTML file then calling the functions I needed from the window window.$pop.render(params)
.我能够通过将其作为脚本标记导入 HTML 文件中然后从 window
window.$pop.render(params)
调用我需要的函数来解决它。 I had to add declare global { interface Window { $pop:any;}}
so TypeScript knew to expect $pop in window.我必须添加
declare global { interface Window { $pop:any;}}
所以 TypeScript 知道在 window 中期望 $pop。 I got this by cominbing these solutions: How do I use external script that I add to react JS?我通过结合这些解决方案得到了这个: 如何使用我添加的外部脚本来响应 JS? and TypeScript error: Property 'X' does not exist on type 'Window' .
和TypeScript 错误:属性 'X' 在类型 'Window' 上不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.