我正在尝试将Google趋势嵌入到React中,以便可以动态调整参数。我可以将其成功直接嵌入到index.html文件中。
<script
type="text/javascript"
src="https://ssl.gstatic.com/trends_nrtr/1644_RC01/embed_loader.js"
></script>
<script type="text/javascript">
trends.embed.renderExploreWidget(
"TIMESERIES",
{
comparisonItem: [{ keyword: "/m/030q7", geo: "", time: "now 7-d" }],
category: 0,
property: ""
},
{
exploreQuery: "q=%2Fm%2F030q7&date=now%207-d",
guestPath: "https://trends.google.com:443/trends/embed/"
}
);
</script>
但是,我需要能够通过用户输入进行更改,因此它必须位于我的jsx文件中。我发现了一个有用的头盔组件,可以帮助添加脚本
<Helmet>
<script
type="text/javascript"
src="https://ssl.gstatic.com/trends_nrtr/1644_RC01/embed_loader.js"
/>
<script type="text/javascript">
{trends.embed.renderExploreWidget(
"TIMESERIES",
{
comparisonItem: [
{ keyword: "/m/030q7", geo: "", time: "now 7-d" }
],
category: 0,
property: ""
},
{
exploreQuery: "q=%2Fm%2F030q7&date=now%207-d",
guestPath: "https://trends.google.com:443/trends/embed/"
}
)}
</script>
</Helmet>
但我越来越喜欢
“趋势”未定义为no-undef
我尝试添加“ this”和“ window”,但出现不同的错误。
有人可以帮助我解决此问题吗?
编辑
尽管提出的解决方案无法直接为我服务,但我还是设法通过采用生成的iframe广告代码并实现了该广告代码来使其正常工作。
<iframe
id="trends-widget-2"
src="https://trends.google.com:443/trends/embed/explore/TIMESERIES?req=%7B%22comparisonItem%22%3A%5B%7B%22keyword%22%3A%22bitcoin%22%2C%22geo%22%3A%22US%22%2C%22time%22%3A%22today%2012-m%22%7D%5D%2C%22category%22%3A0%2C%22property%22%3A%22%22%7D&tz=-480&eq=q%3Dbrexit%26geo%3DUS%26date%3Dtoday%2012-m"
width="100%"
height="300px"
frameborder="0"
scrolling="0"
/>
答案 0 :(得分:3)
我意识到这个问题是在一段时间之前发布的,但是我想分享Google趋势的可重用组件,以及如何将其嵌入到react组件中,以防将来对任何人有帮助。为了能够加载脚本,我使用了react-load-script库。
这是代码框:
https://codesandbox.io/embed/competent-cookies-v2kjz?fontsize=14&hidenavigation=1&theme=dark
这是代码:
// App.js
import React from "react";
import GoogleTrends from "./GoogleTrends";
import "./styles.css";
export default function App() {
return (
<>
<h1> Google Trends Example</h1>
<p>
Put the GoogleTrends comonent in a div with an id. Use the id, e.g.
"widget" inside document.getElementById("widget") in the GoogleTrends
component.
</p>
<div id="widget">
<GoogleTrends
type="TIMESERIES"
keyword="Celine Dion"
url="https://ssl.gstatic.com/trends_nrtr/2051_RC11/embed_loader.js"
/>
<GoogleTrends
type="GEO_MAP"
keyword="Celine Dion"
url="https://ssl.gstatic.com/trends_nrtr/2051_RC11/embed_loader.js"
/>
</div>
</>
);
}
// GoogleTrends.js
import React from "react";
import ReactDOM from "react-dom";
import Script from "react-load-script";
export default function GoogleTrends({ type, keyword, url }) {
const handleScriptLoad = _ => {
window.trends.embed.renderExploreWidgetTo(
document.getElementById("widget"),
type,
{
comparisonItem: [{ keyword, geo: "US", time: "today 12-m" }],
category: 0,
property: ""
},
{
exploreQuery: `q=${encodeURI(keyword)}&geo=US&date=today 12-m`,
guestPath: "https://trends.google.com:443/trends/embed/"
}
);
};
const renderGoogleTrend = _ => {
return <Script url={url} onLoad={handleScriptLoad} />;
};
return <div className="googleTrend">{renderGoogleTrend()}</div>;
}
答案 1 :(得分:2)
首先将脚本网址加载到您的html文件中。然后使用npm中的react-script-tag,然后将其写在组件的渲染器上:
<ScriptTag type="text/javascript">
{'trends.embed.renderExploreWidgetTo(
document.getElementById('widget'),
'TIMESERIES',
{'comparisonItem': [{'keyword':'chicken','geo':'','time':'today 12-m'}],'category':0,'property':''},
{'exploreQuery':'q=chicken&date=today 12-m','guestPath':'https://trends.google.com:443/trends/embed/'}
)'}
</ScriptTag>