【发布时间】:2016-10-04 09:58:42
【问题描述】:
我正在尝试将 LinkedIn login 集成到 Angular 2 中,一切正常,但我在 webpack 中遇到的错误很少,下面是我的代码和我遇到的错误:
//linkedin.component.ts
constructor(private zone: NgZone) {
window.angularComponentRef = {
zone: this.zone,
componentFn: () => this.onLinkedInLoad(),
component: this
};
}
ngOnInit() {
// Creating script tag with linkedin src and required api details
var linkedinEle = document.createElement('script');
linkedinEle.type = 'text/javascript';
linkedinEle.src = 'http://platform.linkedin.com/in.js';
linkedinEle.innerHTML = 'api_key: xxxxxxxxxxx \n authorize: true \n onLoad: onLinkedInLoad';
document.head.appendChild(linkedinEle);
}
// this will called when the linkedin api gets load
onLinkedInLoad() {
IN.Event.on(IN, "auth", function() {
console.log('Hell yeah');
});
}
// index.html
function onLinkedInLoad() {
window.angularComponentRef.zone.run(function () { window.angularComponentRef.componentFn()})
}
一切正常,但在 webpack 中出现此错误:
1. Property 'angularComponentRef' does not exist on type 'Window'.
2. Cannot find name 'IN'.
所以我想我可以通过像declare let IN: any 一样声明它来解决它,但没有运气。
【问题讨论】:
-
在 ngAfterViewInit 中包含您的代码,而不是 ngOnInit
-
好的,但是问题依然存在
-
将 custruter 中的东西移动到 ngAfterViewInit 中
-
@DheerajAgrawal:您解决了上述问题吗?