简体   繁体   中英

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. There is a web library written in JS I'm trying to use but I'm not sure how to import it. In the old project it was just imported with a script tag in head. The old function called in the embedded JS was let external = $pop.render(params) . The extension for the old import in the script tag was /index.js?mode=api . Any help would be appreciated. Thanks in advance! The library is a custom private js library. I have tried import "URL TO LIBRARY"; , jQuery.getScript() , and import * as Pop from 'URL'; . It is an AMD based module.

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) . I had to add declare global { interface Window { $pop:any;}} so TypeScript knew to expect $pop in window. I got this by cominbing these solutions: How do I use external script that I add to react JS? and TypeScript error: Property 'X' does not exist on type 'Window' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM