相关疑难解决方法(0)

Angular 2+应用程序上的嵌入式Twitter小部件仅在首页加载时显示

如果我将Twitter文档(https://dev.twitter.com/web/javascript/loading)中的内置函数完全复制到ngAfterViewInit函数中,则我的应用程序将运行正常,但是当我切换路线时,小部件也会消失。

以下是仅适用于首页加载的代码:

import { Component, OnInit, OnDestroy, AfterViewInit } from '@angular/core';
import { Router, NavigationEnd, ActivatedRoute, ParamMap, Params } from '@angular/router';
import { Observable, Subscription } from 'rxjs/Rx';

export class ProjectDetailComponent implements OnInit, OnDestroy, AfterViewInit {

constructor(
    private route: ActivatedRoute,
    private router: Router
  ) { }

ngOnInit(){}

ngAfterViewInit(){
  (<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, …
Run Code Online (Sandbox Code Playgroud)

twitter angular-routing twitter-widget angular

1
推荐指数
1
解决办法
936
查看次数