javascript - 为什么在第一页更改/重定向到另一个路由后,React 中的外部 JavaScript 不加载

标签 javascript reactjs react-router

我有一个使用react selly-midtrans.herokuapp.com的静态网站,但正如你所看到的,有一些eksternal js位于index.html中,但问题是当我们将路由更改为另一个菜单时,这些外部菜单会执行当我们第一次访问该页面时不会加载。

我已经阅读了一些 friend 使用 componentDidMount/ComponentWillMountcomponentWillUnmount 的建议,但我仍然对使用该方法附加我的 js 并解决我的问题感到有点困惑

这是我的index.html代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="theme-color" content="#000000">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
    <link href='https://fonts.googleapis.com/css?family=Lato:100,300,400,700,900' rel='stylesheet' type='text/css'>
    <!--
      manifest.json provides metadata used when your web app is added to the
      homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json">
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
    <script async="" defer="" src="https://apis.google.com/js/platform.js?publisherid=105038212373502490985" type="text/javascript"></script>
    <!--Google analytic-->
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-106169293-1', 'auto'); ga('send', 'pageview');
    </script>
    <title>Sell Easily With Selly | Selly</title>
  </head>
  <body>
    <noscript>
      You need to enable JavaScript to run this app.
    </noscript>
    <div id="root"></div>
    <script src="%PUBLIC_URL%/js/jquery.min.js"></script>
    <script src="%PUBLIC_URL%/js/bootstrap.min.js"></script>
    <script src="%PUBLIC_URL%/js/owl.carousel.js"></script>
    <script src="%PUBLIC_URL%/js/main.js"></script>
  </body>
</html>

最佳答案

关于javascript - 为什么在第一页更改/重定向到另一个路由后,React 中的外部 JavaScript 不加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47606784/

相关文章:

javascript - React JSX 中的 ...rest 是什么意思?

javascript - 推送新 URL 时,React 组件渲染会被多次调用

javascript - 没有菜单或主布局的 React Router 第一条路线

javascript - 如何在特定时间禁用点击事件处理程序?

javascript - 在 React 组件类中, "props"对象究竟是从哪里提取的?

javascript - 在jquery数据表服务器端分页中指定页面大小

javascript - --如何让这个 React/Redux 代码变得 DRY

reactjs - 在 Electron 中同时运行前端和后端

javascript - 自动滚动不会滚动到元素,损坏的 JS

javascript - 使跟踪代码从 URL 栏中消失