我试图在我的一个 Angular 组件中添加订阅按钮。
因此,我遵循YouTube Developers提供的标准方法。加载页面时,按钮不显示。我有什么想念的吗?
项目规格:
Angular 5
typescript 2.4
非常感谢您的时间和帮助。
最佳答案
似乎您必须动态添加YouTube脚本。
您可以执行以下操作,首先将html粘贴到您的代码或任何html文件中。
<div class="g-ytsubscribe" data-channel="GoogleDevelopers" data-
layout="full" data-theme="dark" data-count="default"></div>
Thought you are using angular your YouTube button won't get initialized.
我有一个技巧,可以动态附加JavaScript文件,以便对按钮进行初始化。
const node = document.createElement('script');
node.src = "https://apis.google.com/js/platform.js";
node.type = 'text/javascript';
node.async = true;
node.charset = 'utf-8';
document.getElementsByTagName('head')[0].appendChild(node);
这将在您的头部动态添加脚本标签,并且您的按钮将被初始化。This approach you can use for anytime when you need to append js dynamically.
Html code goes into your template file and JS code will goes into your init() function or on click function that dependence on your requirement.
关于angular - Youtube订阅按钮在Angular项目中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49993516/