我已经用 gatsby.js 试验了一段时间,除了这个问题外,一切都很顺利,我不能在应用程序中包含 jQuery 脚本,以便在渲染 gatsby 应用程序后加载,我已经包含了脚本标签文件并加载它,html.js
但似乎代码是在反应将内容呈现到我尝试使用的屏幕之前执行的,simple-load-script
以将其包含componentDidMount
在应用程序的方法中html.js
。但是没有运气,这是我html.js
文件的源代码:
html.js
import React from "react"
import PropTypes from "prop-types"
export default class HTML extends React.Component {
componentDidMount() {
console.log('hello world');
}
render() {
return (
<html {...this.props.htmlAttributes}>
<head>
<meta charSet="utf-8" />
<meta httpEquiv="x-ua-compatible" content="ie=edge" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
{this.props.headComponents}
</head>
<body>
{this.props.preBodyComponents}
<div
key={`body`}
id="___gatsby"
dangerouslySetInnerHTML={{ __html: this.props.body }}
/>
{this.props.postBodyComponents}
</body>
</html>
)
}
}
HTML.propTypes = {
htmlAttributes: PropTypes.object,
headComponents: PropTypes.array,
bodyAttributes: PropTypes.object,
preBodyComponents: PropTypes.array,
body: PropTypes.string,
postBodyComponents: PropTypes.array,
}
如您所见,我替换了componentDidMount()
写入控制台的方法,并且没有阻止该方法执行的东西。
如果有人有这方面的经验,请分享,谢谢。