简体   繁体   English

如何在反应中包含外部javascript

[英]how to include external javascript in react

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> 在html页面中,你可以通过<script src="path"></script>包含一个javascript文件。如果路径是,你如何从这样的外部路径获取变量,例如: <script src="https://unpkg.com/flux-sdk-helpers@0.1/dist/flux-sdk-helpers.min.js"></script>

Thanks for all your help 感谢你的帮助

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. 根据文档 ,当你以这种方式安装时,flux-sdk-helpers将自己安装为全局变量FluxHelpers

This means your React code can access it as either FluxHelpers or window.FluxHelpers . 这意味着您的React代码可以将其作为FluxHelperswindow.FluxHelpers

你可以在你的index.html文件中链接它只是要小心你在做什么时调用的东西,以免在不同的地方使用名称

if you are using react, then you probably should also learn about webpack. 如果您正在使用react,那么您可能还应该了解webpack。

if you will use webpack, then you can import { method} from "package" most scripts you got from NPM (node package manager) very easily. 如果您将使用webpack,那么您可以非常轻松地import { method} from "package"来自NPM(节点包管理器)的大多数脚本的import { method} from "package"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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