简体   繁体   English

在反应应用程序中导入 jQuery 时,如何在内联脚本中使用 jQuery?

[英]How to Use jQuery in a inline script when jQuery was imported in a react app?

I've been learning react and have a simple question.我一直在学习反应并有一个简单的问题。

I created an app component which is working well.我创建了一个运行良好的应用程序组件。

I installed jQuery using npm and import jquery in app component import 'jquery/dist/jquery.min.js'我使用npm并在应用程序组件import jquery import 'jquery/dist/jquery.min.js'中导入 jquery

At index.html file I have a script tag injecting a script.js and I'd like to use jQuery in it.在 index.html 文件中,我有一个注入 script.js 的脚本标签,我想在其中使用 jQuery。 But neither $ nor jQuery has been recognized.但是 $ 和 jQuery 都没有得到认可。

What should be done to make it work?应该怎么做才能让它发挥作用?

I think your node modules jquery not loading in index.html, you can use jquery by link like我认为您的节点模块 jquery 未加载到 index.html,您可以通过链接使用 jquery

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

or put your jquery code in the related component, there you can easily import your jquery package.或将您的 jquery 代码放入相关组件中,您可以轻松地导入您的 jquery package。

import * as $ from "jquery";

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

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