Twitter widget on Angular 2

后端 未结 3 1339
逝去的感伤
逝去的感伤 2020-12-17 06:56

I created a Twitter widget and I\'m trying to put it in my Angular 2 project, but the JS file does not work.

If I insert it with JS (Here) it works, but when I switc

相关标签:
3条回答
  • 2020-12-17 07:15

    Example for my comment (relevant parts):

    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"));
          }
        });
      }
    }
    
    0 讨论(0)
  • 2020-12-17 07:19

    This will work for me on angular 7.2.2 and ionic 4.1. angular can build and ionic can build app and test on cordova.

    solution from : 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);
      }
    }
    

    put here in .ts and put twitter tag in template files

    0 讨论(0)
  • 2020-12-17 07:22

    Alright, I found a solution to this. When we switch between Angular routers, the widget unloads. That's why we put a subscriber, so we load the widget every time we switch between tabs. We can see here how to load the widget for lazy-loading content. When the page is ready, we can use that function to load the widget. Don't forget to unsubscribe after the view is destroyed!

    Code:

    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();
      }
    }
    
    0 讨论(0)
提交回复
热议问题