如何将Twitter时间线嵌入Angular4应用程序

时间:2017-08-21 13:56:21

标签: javascript angular twitter

Angular的新手,请耐心等待。我需要从特定的时间线加载推文。完成此任务的最佳方法是什么?我已尝试使用此软件包(https://www.npmjs.com/package/ng4-twitter-timeline),并按照该文档中的说明进行操作,但我仍然收到“ng4-twitter-timeline不是已知元素”的错误。

我也试过加入

<script src="https://platform.twitter.com/widgets.js" async></script>

到index.html ......

是否需要加载其他脚本才能使其正常工作?

app.module.ts

...
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { RouterModule } from '@angular/router';
import { SwiperModule } from 'angular2-useful-swiper';
import { AngularFontAwesomeModule } from 'angular-font-awesome/angular-font-awesome';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { ShareModule } from 'ng2share/share.module';
import { MasonryModule } from 'angular2-masonry';
import { routes } from './app-routing.module';
import { Ng4TwitterTimelineModule } from 'ng4-twitter-timeline/lib/index';

@NgModule({
  declarations: [
  ...
  ],
  imports: [
     BrowserModule,
     AppRoutingModule,
     HttpModule,
     AngularFontAwesomeModule,
     SwiperModule,
     RouterModule.forRoot(routes),
     ShareModule,
     MasonryModule,
     Ng4TwitterTimelineModule
  ],
    providers: [],
    bootstrap: [AppComponent]
  })
  export class AppModule { }

tweets.component.ts

   import { Component, OnInit } from '@angular/core';
   import { Ng4TwitterTimelineService } from 'ng4-twitter-timeline/lib/index';

  @Component({
    selector: 'app-tweets',
    templateUrl: './tweets.component.html',
    styleUrls: ['./tweets.component.scss']
  })
  export class TweetsComponent implements OnInit {

 constructor(private ng4TwitterTimelineService: Ng4TwitterTimelineService) {}

   ngOnInit() {}
}

tweets.component.html

   <ng4-twitter-timeline [dataSrc]="{sourceType: 'profile',screenName: 'lokers_one'}" [opts]="{tweetLimit: 2}"></ng4-twitter-timeline>

2 个答案:

答案 0 :(得分:1)

在@ngModule中导入Ng4TwitterTimelineModule时,应添加.forRoot。所以你的app.module.ts应该是这样的:

...
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { RouterModule } from '@angular/router';
import { SwiperModule } from 'angular2-useful-swiper';
import { AngularFontAwesomeModule } from 'angular-font-awesome/angular-font-awesome';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { ShareModule } from 'ng2share/share.module';
import { MasonryModule } from 'angular2-masonry';
import { routes } from './app-routing.module';
import { Ng4TwitterTimelineModule } from 'ng4-twitter-timeline/lib/index';

@NgModule({
  declarations: [
  ...
  ],
  imports: [
     BrowserModule,
     AppRoutingModule,
     HttpModule,
     AngularFontAwesomeModule,
     SwiperModule,
     RouterModule.forRoot(routes),
     ShareModule,
     MasonryModule,
     Ng4TwitterTimelineModule.forRoot()
  ],
    providers: [],
    bootstrap: [AppComponent]
  })
  export class AppModule { }

答案 1 :(得分:0)

想出来,大家。演示中使用的帐户有保护的推文...这就是为什么没有嵌入...我更改了帐户,它确实有效。但仍然不明白为什么我仍然得到这个错误:'ng4-twitter-timeline'不是一个已知元素'