gpt4 book ai didi

angular - 这是将 Disqus 嵌入 Angular2 组件的正确方法吗?

转载 作者:行者123 更新时间:2023-12-02 22:03:58 27 4
gpt4 key购买 nike

以下是否正确地将 Disqus 嵌入到 Angular2 组件中

disqus.component.html

<div class="card card-block">
<div id="disqus_thread"></div>
<noscript>
Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript" rel="nofollow">comments powered by Disqus.</a>
</noscript>
</div>

--

disqus.component.ts

//Above code omitted for simplicity

export class DisqusComponent {
ngOnInit(){
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');

s.src = '//blahblahblahdafsfawf.disqus.com/embed.js';

s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
}
}

最佳答案

更新:最简单的方法是使用这个 DisqusModule,您可以从 npm check Live Demo 安装它。

如果您仍然想自己编写它,这个组件就可以完成工作

import {Component, Input, ElementRef, OnInit, Renderer2} from '@angular/core';


@Component({
selector: 'disqus',
template: '<div id="disqus_thread"></div>',
})

export class Disqus implements OnInit{

@Input() public identifier:string;
@Input() public shortname:string;

constructor(private el:ElementRef, private renderer:Renderer2) {
this.dom = el.nativeElement;
}

ngOnInit() {
if ((<any>window).DISQUS === undefined) {
this.addScriptTag();
}
else {
this.reset();
}
}

/**
* Reset Disqus with new information.
*/
reset() {
(<any>window).DISQUS.reset({
reload: true,
config: this.getConfig()
});
}

/**
* Add the Disqus script to the document.
*/
addScriptTag() {
(<any>window).disqus_config = this.getConfig();

let script = this.renderer.createElement(this.el.nativeElement, 'script');
script.src = `//${this.shortname}.disqus.com/embed.js`;
script.async = true;
script.type = 'text/javascript';
script.setAttribute('data-timestamp', new Date().getTime().toString());
}

/**
* Get Disqus config
*/
getConfig() {
let _self = this;
return function () {
this.page.url = window.location.href;
this.page.identifier = _self.identifier;
this.language = 'en';
};
}
}

关于angular - 这是将 Disqus 嵌入 Angular2 组件的正确方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36102556/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com