how to include external javascript in react

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-27 13:02:44

问题


In an html page you can include a javascript file by <script src="path"></script> How do you get variables from an external path like this in react, if the path is, for example: <script src="https://unpkg.com/flux-sdk-helpers@0.1/dist/flux-sdk-helpers.min.js"></script>

Thanks for all your help


回答1:


Scripts loaded in that fashion normally install themselves as global variables and/or properties of some other global library.

According to the docs, flux-sdk-helpers installs itself as a global variable FluxHelpers when you install it this way.

This means your React code can access it as either FluxHelpers or window.FluxHelpers.




回答2:


you can link it in your index.html file just be careful about what you call things when you are doing it as to not re use names in different places




回答3:


if you are using react, then you probably should also learn about webpack.

if you will use webpack, then you can import { method} from "package" most scripts you got from NPM (node package manager) very easily.



来源:https://stackoverflow.com/questions/43989455/how-to-include-external-javascript-in-react

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!