javascript - How to add external js file in react application? - Stack Overflow

I have created one js file in our application. in this js file How add in our page?I had Written below

I have created one js file in our application. in this js file How add in our page?

I had Written below code for our application. First of I had written online link that's work perfect. then after download js and set path to js file. but not proper working.

ponentDidMount() {
const script = document.createElement("script");
// script.src = ".12.4.min.js";
script.src = "../../Content/JS/jquery-1.12.4.min.js";
script.async = true;
document.body.appendChild(script);

}

I have created one js file in our application. in this js file How add in our page?

I had Written below code for our application. First of I had written online link that's work perfect. then after download js and set path to js file. but not proper working.

ponentDidMount() {
const script = document.createElement("script");
// script.src = "https://code.jquery./jquery-1.12.4.min.js";
script.src = "../../Content/JS/jquery-1.12.4.min.js";
script.async = true;
document.body.appendChild(script);

}

Share Improve this question asked Jan 7, 2019 at 6:19 Diamond KingDiamond King 67610 silver badges17 bronze badges 5
  • install those packages by npm install {package name} – Vikas Singh Commented Jan 7, 2019 at 6:21
  • which one package install ?? – Diamond King Commented Jan 7, 2019 at 6:23
  • as i can see you want to use Jquery in project then you can install it by npm install --save jquery or through your package manager – Vikas Singh Commented Jan 7, 2019 at 6:25
  • I have already install this package. but can't add to external js file. – Diamond King Commented Jan 7, 2019 at 6:32
  • put js file within src folder and import this file and put it in script tag anywhere inside your project. – Mustkeem K Commented Jan 7, 2019 at 6:34
Add a ment  | 

2 Answers 2

Reset to default 2

If you want to add additional js file (script). This is the easiest way to to do this.

<body>
<noscript>
  You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>

<script src="../../Content/JS/jquery-1.12.4.min.js"></script>

</body>

This is the root html file of your react application. According to your folder structure path can be changed.

You need to install and then import jQuery or any other plugin you need in your ponent.

npm install {package name}

in your package JSON.

import jquery from 'jquery'

in your ponent

Here is an example

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745345345a4623518.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信