我正在尝试将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'
有什么想法吗?
答案 0 :(得分:3)
弄清楚了。您需要在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);
};
}
希望这可以帮助任何坚持同一件事的人。
答案 1 :(得分:0)
对于将来的读者,Trustpilot现在提供了有关单页应用程序的文档,您可以找到here
我想知道为什么即使他们的官方文档都什么也没渲染,但是经过2个小时的痛苦,我发现(在我撰写本文时)您无法将他们的小部件加载到Brave导航器上... >