angular - 如何在运行时在 Angular 中添加 Freshdesk 小部件?
问题描述
我正在使用 Angular 9 并尝试将 Freshdesk 小部件添加到我的应用程序中。该小部件的片段如下所示:
<script>
window.fwSettings={
'widget_id': <MY PERSONAL ID>,
'locale': 'en'
};
!function(){if("function"!=typeof window.FreshworksWidget){var n=function(){n.q.push(arguments)};n.q=[],window.FreshworksWidget=n}}()
如果我将代码段直接添加到我的index.html
文件中,它可以正常工作,但我希望能够以编程方式将locale
属性设置为当前用户选择的任何语言。
这意味着这个片段需要在运行时添加,但我不知道如何在 Angular 中添加它。
有任何想法吗?
解决方案
我解决了。在我app.component.ts
加载我的应用程序区域设置后,我将代码段放入一个函数中:
loadFreshDesk(locale): void {
window.fwSettings = {
widget_id: MY_WIDGET_ID,
locale: MY_FRESH_DESK_LOCALE,
};
!(function () {
if ('function' !== typeof window.FreshworksWidget) {
const n = function () {
n.q.push(arguments);
};
(n.q = []), (window.FreshworksWidget = n);
}
})();
const script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://widget.freshworks.com/widgets/MY_WIDGET_ID.js';
document.getElementsByTagName('head')[0].appendChild(script);
}
推荐阅读
- facebook - Vue 参数在 Instagram、Facebook 等应用内浏览器中丢失
- java - 如何在java中将时间格式转换为日期格式的今天时间?
- javascript - 检查对象数组是否存在
- google-apps-script - 每当脚本运行时,如何更改 onEdit 函数?- 谷歌表格
- android - 启动对话框时如何显示键盘?
- go - 为什么 context.Context 信号会影响新的 goroutine?
- node.js - 如何在每次刷新页面时验证会话 cookie 的完整性,应该这样做吗?
- pandas - 熊猫石斑鱼缺少时间步长
- node.js - Node.js 连接到 Squelize
- javascript - 如何将时间值从一个文本框设置到另一个邪恶的时间选择器?