如何在 React 创建应用程序中包含自定义 JS 文件 [英] How to include custom JS files in to React create app

查看:58
本文介绍了如何在 React 创建应用程序中包含自定义 JS 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在公共文件夹下有 js 文件,我想将这些 js 文件包含在 React 创建应用程序中 src 组件文件夹下的组件中?

I had js files under public folder i want to include these js files in to react components which are under src component folder in react create application?

推荐答案

请阅读 使用 public 文件夹.

Please read Using the public Folder.

一般来说,我们建议从 npm 安装包并使用 import 从源代码访问它们.但是,如果您必须使用不想导入的预构建 JS 文件,则可以将其添加到 public 文件夹(例如,public/mylib.js)然后将其添加到 public/index.html:

In general we recommend installing packages from npm and using import to access them from the source. But if you must use a prebuilt JS file that you don’t want to import, you can add it to the public folder (for example, public/mylib.js) and then add this to public/index.html:

<script src="%PUBLIC_URL%/mylib.js"></script>

然后您可以从您的应用程序访问全局变量,例如:

Then you can access the global variable from your app, for example:

const mylib = window.mylib;

然后使用它.

希望这会有所帮助!

这篇关于如何在 React 创建应用程序中包含自定义 JS 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆