简体   繁体   English

在React组件中使用静态脚本标签

[英]Using a static script tag in a React component

I'm using the default starting project from create-react-app , with the following folder structure: 我正在使用create-react-app的默认启动项目,其具有以下文件夹结构:

app
--public
----index.html
--src
----App.js
----components
-------map.js

I have included an external CDN library in the index.html body tag. 我在index.html body标签中包含了一个外部CDN库。 However, I'm confused about how to use the methods of this library in my map.js component. 但是,我对如何在map.js组件中使用该库的方法感到困惑。

Just writing the library methods in my map.js , returns: 只需在map.js编写库方法, map.js返回:

Failed to compile
./src/components/map.js
  Line 5:  'L' is not defined  no-undef 

Is there any way I can include a JS CDN library in my React components without actually importing the library as React Component via npm install ? 有什么方法可以在我的React组件中包含一个JS CDN库,而无需通过npm install将该库实际导入为React Component呢?

You're loading a component in the DOM, React has no knowledge of the DOM. 您正在DOM中加载组件,React不了解DOM。 So you need to create a wrapper component for the DOM library and use the ref prop to access it. 因此,您需要为DOM库创建一个包装器组件,并使用ref属性访问它。 If you're a beginner please use React libraries exclusively, if you need third party libraries, start by reading the React docs https://reactjs.org/docs/integrating-with-other-libraries.html about third party libraries. 如果您是初学者,请仅使用React库,如果您需要第三方库,请先阅读关于第三方库的React docs https://reactjs.org/docs/integrating-with-other-libraries.html

Btw, the error you're seeing is most likely a babel error which can't transform your ES6 classes, it can't find the library you require. 顺便说一句,您看到的错误很可能是babel错误,无法转换您的ES6类,找不到您需要的库。 I think there are options to configure global environment, so it ignores these errors (but that probably means ejecting from cra). 我认为有配置全局环境的选项,因此它会忽略这些错误(但这可能意味着从cra弹出)。

Take a look at this link: 看一下这个链接:

https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#using-the-public-folder https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#using-the-public-folder

To reference assets in the public folder, you need to use a special variable called PUBLIC_URL. 要引用公用文件夹中的资产,您需要使用一个名为PUBLIC_URL的特殊变量。

Inside index.html, you can use it like this: 在index.html中,您可以像这样使用它:

Only files inside the public folder will be accessible by %PUBLIC_URL% prefix. %PUBLIC_URL%前缀只能访问公用文件夹中的文件。 If you need to use a file from src or node_modules, you'll have to copy it there to explicitly specify your intention to make this file a part of the build. 如果您需要使用src或node_modules中的文件,则必须将其复制到此处以明确指定打算将此文件作为构建的一部分。

When you run npm run build, Create React App will substitute %PUBLIC_URL% with a correct absolute path so your project works even if you use client-side routing or host it at a non-root URL. 当您运行npm run build时,Create React App将用正确的绝对路径替换%PUBLIC_URL%,因此即使您使用客户端路由或将其托管在非根URL上,您的项目也能正常工作。

In JavaScript code, you can use process.env.PUBLIC_URL for similar purposes: 在JavaScript代码中,可以将process.env.PUBLIC_URL用于类似目的:

It will show you how to do it the way create-react-app prefers. 它将向您展示如何按照create-react-app的偏好进行操作。

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

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