我有一个使用react selly-midtrans.herokuapp.com的静态网站,但正如你所看到的,有一些eksternal js位于index.html中,但问题是当我们将路由更改为另一个菜单时,这些外部菜单会执行当我们第一次访问该页面时不会加载。
我已经阅读了一些 friend 使用 componentDidMount/ComponentWillMount
和 componentWillUnmount
的建议,但我仍然对使用该方法附加我的 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>
最佳答案
访问How do you embed and call an external javascript function in a react component?
访问https://discuss.reactjs.org/t/using-external-script-libraries/2256
关于javascript - 为什么在第一页更改/重定向到另一个路由后,React 中的外部 JavaScript 不加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47606784/