首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Next.js中的Trustpilot TrustBoxes

Next.js中的Trustpilot TrustBoxes
EN

Stack Overflow用户
提问于 2018-09-24 17:56:26
回答 2查看 1.6K关注 0票数 0

我正在尝试将Trustpilot TrustBox添加到Next.js应用程序中。

我的componentDidMount中有以下内容:

代码语言:javascript
复制
 var trustbox = document.getElementById('trustbox');
 window.Trustpilot.loadFromElement(trustbox);

在我的脑海里:

代码语言:javascript
复制
<script type="text/javascript" src="//widget.trustpilot.com/bootstrap/v5/tp.widget.bootstrap.min.js" async></script>

在我的html中:

代码语言:javascript
复制
<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“”

有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2018-09-24 19:44:57

我想通了。您需要在componentDidMount中使用以下代码来确保首先加载外部js。

代码语言:javascript
复制
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);
    };
}

希望这篇文章能帮助那些被困在同一件事上的人。

票数 7
EN

Stack Overflow用户

发布于 2020-10-01 21:21:17

对于未来的读者,Trustpilot现在提供了单页应用程序的文档,您可以在其中找到here

我想知道为什么他们的官方文档都没有渲染,但经过2个小时的痛苦之后,我发现(在我写这篇文章的时候)你不能在Brave navigator上加载他们的小部件……

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52476769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档