简体   繁体   English

无法在React JS中导入外部javascript(jQuery)文件?

[英]External javascript(jQuery) file could not be import in react js?

I have write a document ready event in my external script file. 我已经在外部脚本文件中编写了文档就绪事件。 And i have include it in my index.html file using <script type="text/javascript" src="/path/to/script.js" But the events not triggered even the file path is correct. 而且我已经使用<script type="text/javascript" src="/path/to/script.js"将其包含在index.html文件中,但是即使文件路径正确,事件也不会触发。

If i put my script code in index.html using <script> //my code </script> the event working properly after browser loading the page. 如果我使用<script> //my code </script>将脚本代码放在index.html中,则该事件在浏览器加载页面后可以正常工作。 But if i redirect to another page using react-router the event not working. 但是,如果我使用react-router重定向到另一个页面,则该事件将无法正常工作。

I am using 我在用

React 16.3.2 反应16.3.2

JQuery 2.2.4 jQuery的2.2.4

Thanks in advance 提前致谢

Actually using jquery with a react app is not a good idea but even so, did you try using " defer " attribute? 实际上,将React与React应用程序一起使用并不是一个好主意,但是即使如此,您是否尝试使用“ defer ”属性?

<script type="text/javascript" src="/path/to/script.js" defer>

and if you want to call your method on every client side page load you should implement something like this: 如果要在每次客户端页面加载时调用方法,则应实现以下内容:

import { browserHistory } from 'react-router';

//Your initialization

browserHistory.listen( location =>  {
 //Do your stuff here
});

wrap all your codes with a function and call it in the listener. 用函数包装所有代码,然后在侦听器中调用它。

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

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