Angular 2 上的 Twitter 小部件
Twitter widget on Angular 2
我创建了一个 Twitter 小部件,并试图将其放入我的 Angular 2 项目中,但 JS 文件不起作用。
如果我用 JS (Here) 插入它,它可以工作,但是当我切换到另一个选项卡并返回时(我安装了 Angular 路由)它消失并显示 A 标签中的内容(所以,"Tweets by ...")。
HTML 代码(在 home 组件中):
<md-card-content>
<a class="twitter-timeline" data-lang="en" data-height="350" data-theme="light" data-chrome="noborders nofooter noheader noscrollbar transparent" href="https://twitter.com/profile">Tweets by profile</a>
</md-card-content>
home.component.ts中的脚本,代码由@Arg0n 提供(查看答案部分):
import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
constructor(private _router : Router) { }
public ngOnInit() {
this._router.events.subscribe(val => {
if (val instanceof NavigationEnd) {
(<any>window).twttr = (function (d, s, id) {
let js: any, fjs = d.getElementsByTagName(s)[0],
t = (<any>window).twttr || {};
if (d.getElementById(id)) return t;
js = d.createElement(s);
js.id = id;
js.src = "https://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
t._e = [];
t.ready = function (f: any) {
t._e.push(f);
};
return t;
}(document, "script", "twitter-wjs"));
twttr.widgets.load();
}
})
}
}
编辑:
好吧,我做了一些研究,发现我必须使用 twttr.widgets.load();
再次加载小部件,但是这样做会引发错误。
我编辑了上面的代码,所以你可以看到我尝试了什么。上面的代码returns一个错误:
[ts] Cannot find name 'twttr'
如果我追加 (window)。对它,它给了我这个:
EXCEPTION: Uncaught (in promise): TypeError: Cannot read property 'load' of undefined
有关加载 属性 的文档在此处:https://dev.twitter.com/web/javascript/initialization
我的评论示例(相关部分):
import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
@Component({ ... })
export class MyComponent implements OnInit {
constructor(private _router: Router) {}
public ngOnInit() {
this._router.events.subscribe(val => {
if (val instanceof NavigationEnd) {
(<any>window).twttr = (function (d, s, id) {
let js: any, fjs = d.getElementsByTagName(s)[0],
t = (<any>window).twttr || {};
if (d.getElementById(id)) return t;
js = d.createElement(s);
js.id = id;
js.src = "https://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
t._e = [];
t.ready = function (f: any) {
t._e.push(f);
};
return t;
}(document, "script", "twitter-wjs"));
}
});
}
}
好的,我找到了解决办法。当我们在 Angular 个路由器之间切换时,小部件将卸载。这就是我们放置订阅者的原因,因此每次在选项卡之间切换时我们都会加载小部件。我们可以看到 here 如何加载延迟加载内容的小部件。页面准备就绪后,我们可以使用该函数来加载小部件。视图销毁后不要忘记取消订阅!
代码:
import { Component, OnDestroy } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
@Component({ ... })
export class HomeComponent implements OnDestroy {
private twitter: any;
constructor(private _router: Router) {
this.initTwitterWidget();
}
initTwitterWidget() {
this.twitter = this._router.events.subscribe(val => {
if (val instanceof NavigationEnd) {
(<any>window).twttr = (function (d, s, id) {
let js: any, fjs = d.getElementsByTagName(s)[0],
t = (<any>window).twttr || {};
if (d.getElementById(id)) return t;
js = d.createElement(s);
js.id = id;
js.src = "https://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
t._e = [];
t.ready = function (f: any) {
t._e.push(f);
};
return t;
}(document, "script", "twitter-wjs"));
if ((<any>window).twttr.ready())
(<any>window).twttr.widgets.load();
}
});
}
ngOnDestroy() {
this.twitter.unsubscribe();
}
}
这适用于 angular 7.2.2 和 ionic 4.1。 angular 可以构建并且 ionic 可以构建应用程序并在 cordova 上进行测试。
解决方案来自:embedded Twitter widget on Angular 2+ app only shows up on the first page load
import { Component, OnInit, OnDestroy, AfterViewInit, PLATFORM_ID, Inject } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
constructor(@Inject(PLATFORM_ID) private platformId: Object) {}
ngAfterViewInit() {
if (isPlatformBrowser(this.platformId)) {
setTimeout(function() {
(<any>window).twttr = (function(d, s, id) {
let js, fjs = d.getElementsByTagName(s)[0], t = (<any>window).twttr || {};
if (d.getElementById(id)) return t;
js = d.createElement(s);
js.id = id;
js.src = 'https://platform.twitter.com/widgets.js';
fjs.parentNode.insertBefore(js, fjs);
t._e = [];
t.ready = function(f) {
t._e.push(f);
};
return t;
}(document, 'script', 'twitter-wjs'));
(<any>window).twttr.widgets.load();
}, 100);
}
}
在此处放入 .ts 并在模板文件中放入 twitter 标签
我创建了一个 Twitter 小部件,并试图将其放入我的 Angular 2 项目中,但 JS 文件不起作用。
如果我用 JS (Here) 插入它,它可以工作,但是当我切换到另一个选项卡并返回时(我安装了 Angular 路由)它消失并显示 A 标签中的内容(所以,"Tweets by ...")。
HTML 代码(在 home 组件中):
<md-card-content>
<a class="twitter-timeline" data-lang="en" data-height="350" data-theme="light" data-chrome="noborders nofooter noheader noscrollbar transparent" href="https://twitter.com/profile">Tweets by profile</a>
</md-card-content>
home.component.ts中的脚本,代码由@Arg0n 提供(查看答案部分):
import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
constructor(private _router : Router) { }
public ngOnInit() {
this._router.events.subscribe(val => {
if (val instanceof NavigationEnd) {
(<any>window).twttr = (function (d, s, id) {
let js: any, fjs = d.getElementsByTagName(s)[0],
t = (<any>window).twttr || {};
if (d.getElementById(id)) return t;
js = d.createElement(s);
js.id = id;
js.src = "https://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
t._e = [];
t.ready = function (f: any) {
t._e.push(f);
};
return t;
}(document, "script", "twitter-wjs"));
twttr.widgets.load();
}
})
}
}
编辑:
好吧,我做了一些研究,发现我必须使用 twttr.widgets.load();
再次加载小部件,但是这样做会引发错误。
我编辑了上面的代码,所以你可以看到我尝试了什么。上面的代码returns一个错误:
[ts] Cannot find name 'twttr'
如果我追加 (window)。对它,它给了我这个:
EXCEPTION: Uncaught (in promise): TypeError: Cannot read property 'load' of undefined
有关加载 属性 的文档在此处:https://dev.twitter.com/web/javascript/initialization
我的评论示例(相关部分):
import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
@Component({ ... })
export class MyComponent implements OnInit {
constructor(private _router: Router) {}
public ngOnInit() {
this._router.events.subscribe(val => {
if (val instanceof NavigationEnd) {
(<any>window).twttr = (function (d, s, id) {
let js: any, fjs = d.getElementsByTagName(s)[0],
t = (<any>window).twttr || {};
if (d.getElementById(id)) return t;
js = d.createElement(s);
js.id = id;
js.src = "https://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
t._e = [];
t.ready = function (f: any) {
t._e.push(f);
};
return t;
}(document, "script", "twitter-wjs"));
}
});
}
}
好的,我找到了解决办法。当我们在 Angular 个路由器之间切换时,小部件将卸载。这就是我们放置订阅者的原因,因此每次在选项卡之间切换时我们都会加载小部件。我们可以看到 here 如何加载延迟加载内容的小部件。页面准备就绪后,我们可以使用该函数来加载小部件。视图销毁后不要忘记取消订阅!
代码:
import { Component, OnDestroy } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
@Component({ ... })
export class HomeComponent implements OnDestroy {
private twitter: any;
constructor(private _router: Router) {
this.initTwitterWidget();
}
initTwitterWidget() {
this.twitter = this._router.events.subscribe(val => {
if (val instanceof NavigationEnd) {
(<any>window).twttr = (function (d, s, id) {
let js: any, fjs = d.getElementsByTagName(s)[0],
t = (<any>window).twttr || {};
if (d.getElementById(id)) return t;
js = d.createElement(s);
js.id = id;
js.src = "https://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
t._e = [];
t.ready = function (f: any) {
t._e.push(f);
};
return t;
}(document, "script", "twitter-wjs"));
if ((<any>window).twttr.ready())
(<any>window).twttr.widgets.load();
}
});
}
ngOnDestroy() {
this.twitter.unsubscribe();
}
}
这适用于 angular 7.2.2 和 ionic 4.1。 angular 可以构建并且 ionic 可以构建应用程序并在 cordova 上进行测试。
解决方案来自:embedded Twitter widget on Angular 2+ app only shows up on the first page load
import { Component, OnInit, OnDestroy, AfterViewInit, PLATFORM_ID, Inject } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
constructor(@Inject(PLATFORM_ID) private platformId: Object) {}
ngAfterViewInit() {
if (isPlatformBrowser(this.platformId)) {
setTimeout(function() {
(<any>window).twttr = (function(d, s, id) {
let js, fjs = d.getElementsByTagName(s)[0], t = (<any>window).twttr || {};
if (d.getElementById(id)) return t;
js = d.createElement(s);
js.id = id;
js.src = 'https://platform.twitter.com/widgets.js';
fjs.parentNode.insertBefore(js, fjs);
t._e = [];
t.ready = function(f) {
t._e.push(f);
};
return t;
}(document, 'script', 'twitter-wjs'));
(<any>window).twttr.widgets.load();
}, 100);
}
}
在此处放入 .ts 并在模板文件中放入 twitter 标签