简体   繁体   English

如何将外部库导入 ReactJS 应用程序(本地)

[英]How to import an external library into a ReactJS Application (Locally)

I've a ReactJS application, and I'm using MaterializeCSS through react-materialize.我有一个 ReactJS 应用程序,我通过 react-materialize 使用 MaterializeCSS。 I've installed react-materialized and added the references to MaterializeCSS files in my index.html file, as ordered:我已经安装了 react-materialized 并在我的 index.html 文件中添加了对 MaterializeCSS 文件的引用,如下所示:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
(...)
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>

It's working fine, but I will spend 10 days in a place with no internet, and I'd like to develop my project there, so the CDN hosted file won't work.它工作正常,但我将在没有互联网的地方度过 10 天,我想在那里开发我的项目,因此 CDN 托管文件将无法工作。 So I'm trying to host materialize files in my own project.所以我试图在我自己的项目中托管物化文件。

I've managed to do that with css files, copied it to a folder in my project structure and imported in App.js:我已经成功地使用 css 文件做到了这一点,将其复制到我的项目结构中的一个文件夹中并导入到 App.js 中:

import './resources/materialize/css/materialize.css';

But when I tried to do the same with .js file, I get tons of errors, since it's trying to transpile materialize.js files.但是当我尝试对 .js 文件做同样的事情时,我得到了很多错误,因为它试图转译 materialize.js 文件。

Where should I put this file and how can I reference it from my project?我应该把这个文件放在哪里,我如何从我的项目中引用它?

You can install it via npm using windows command prompt, simply Start > Run > cmd, locate your project folder via cd <foldername> and type this:您可以使用 Windows 命令提示符通过 npm 安装它,只需开始 > 运行 > cmd,通过cd <foldername>找到您的项目文件夹并键入以下内容:

npm install --save materialize-css@next

and at your Index.js : - which is in the root folder of your application并在您的Index.js : - 位于您的应用程序的根文件夹中

import 'materialize-css';
import 'materialize-css/dist/css/materialize.min.css';

I also strongly recommend material UI which is very convenient with react applications: https://material-ui.com/我也强烈推荐使用 React 应用程序非常方便的 Material UI: https : //material-ui.com/

you can run你可以跑

npm install --save materialize-css@next

and then you can add it in the index.js file the following import:然后你可以在 index.js 文件中添加以下导入:

import 'materialize-css'; //This adds the Js file
import 'materialize-css/dist/css/materialize.min.css';

A simpler solution, to avoid potentially complex webpack setups is to temporarly download the actual files from their website:为了避免潜在的复杂 webpack 设置,一个更简单的解决方案是从他们的网站临时下载实际文件:

https://materializecss.com/getting-started.html https://materializecss.com/getting-started.html

And just place their file in your js folder and reference it via script tag, exactly as they show in their "Setup - Project Structure" section.只需将他们的文件放在您的 js 文件夹中并通过脚本标记引用它,正如它们在“设置 - 项目结构”部分中显示的那样。

I know it's not the most elegant solution but it might work if you are in a rush, otherwise you need to look how to include the project with webpack without it touching its code.我知道这不是最优雅的解决方案,但如果您匆忙,它可能会起作用,否则您需要查看如何将项目包含在 webpack 中而不触及其代码。

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

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