如何在gatsby项目中包含自定义javascript文件? [英] How to include custom javascript file in gatsby project?

查看:184
本文介绍了如何在gatsby项目中包含自定义javascript文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向我的Gatsby网站添加自定义javascript文件,但是我对Gatsby还是很陌生,并且不确定实现它的最佳方法是什么以及如何实现.我尝试了不同的方法,但仍然出现错误:

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:

未捕获的ReferenceError:jQuery未定义

Uncaught ReferenceError: jQuery is not defined

推荐答案

首先,Gatsby是React的静态站点生成器,通常我们不会使用React来实现JQuery.如果您想通过JQuery操作DOM,我想您可能可以通过React来做同样的事情.

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.

回到您的问题,盖茨比(Gatsby)通常包含几种方法来包含JS文件:

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

第一个是将您的脚本包含在Gatsby的 html.js 中.您可以在此处参考文档: https://www.gatsbyjs.org/docs/custom-html /#adding-custom-javascript

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

第二个使用的是反应头盔.它可以将head标签嵌入到您的组件中. https://www.npmjs.com/package/react-helmet

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

这篇关于如何在gatsby项目中包含自定义javascript文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆