我正在尝试将Trustpilot TrustBox添加到Next.js应用程序中。
我的componentDidMount中有以下内容:
var trustbox = document.getElementById('trustbox');
window.Trustpilot.loadFromElement(trustbox);在我的脑海里:
<script type="text/javascript" src="//widget.trustpilot.com/bootstrap/v5/tp.widget.bootstrap.min.js" async></script>在我的html中:
<div id="trustbox" className="trustpilot-widget" data-locale="en-GB" data-template-id="XX" data-businessunit-id="XX" data-style-height="130px" data-style-width="100%" data-theme="light" data-stars="5" data-schema-type="Organization">
<a href="https://uk.trustpilot.com/review/XX" target="_blank">Trustpilot</a>
</div>这在热重新加载时工作得很好。例如,如果我在服务器运行时添加代码。但是,o刷新重新加载它会中断,我会得到以下错误:
无法读取未定义的属性“”loadFromElement“”
有什么想法吗?
发布于 2018-09-24 19:44:57
我想通了。您需要在componentDidMount中使用以下代码来确保首先加载外部js。
componentDidMount() {
var aScript = document.createElement('script');
aScript.type = 'text/javascript';
aScript.src = "//widget.trustpilot.com/bootstrap/v5/tp.widget.bootstrap.min.js";
aScript.async = "true"
document.head.appendChild(aScript);
aScript.onload = function () {
var trustbox = document.getElementById('trustbox');
window.Trustpilot.loadFromElement(trustbox);
};
}希望这篇文章能帮助那些被困在同一件事上的人。
发布于 2020-10-01 21:21:17
对于未来的读者,Trustpilot现在提供了单页应用程序的文档,您可以在其中找到here
我想知道为什么他们的官方文档都没有渲染,但经过2个小时的痛苦之后,我发现(在我写这篇文章的时候)你不能在Brave navigator上加载他们的小部件……
https://stackoverflow.com/questions/52476769
复制相似问题