How to include custom javascript file in gatsby project? [duplicate]

自古美人都是妖i 提交于 2019-12-27 04:48:45

问题


I'm trying to add a custom javascript file to my Gatsby site but I'm still very new to Gatsby and not sure what the best way is to implement it and how. I have tried different methods but I keep getting an error:

Uncaught ReferenceError: jQuery is not defined


回答1:


First of all, Gatsby is a static site generator of React and usually we won't implement JQuery with React. If you would like to manipulate your DOM by JQuery, I think you probably could do the same by React.

Back to your question, there are several ways commonly used to include a JS file in Gatsby:

The first one is include your script in Gatsby's html.js. You can refer the documentation there: https://www.gatsbyjs.org/docs/custom-html/#adding-custom-javascript

The second one is using React Helmet. It will let to embed head tags in your components. https://www.npmjs.com/package/react-helmet



来源:https://stackoverflow.com/questions/57612229/how-to-include-custom-javascript-file-in-gatsby-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!