CdkVirtualScrollViewport已附加(错误)

时间:2019-01-29 10:36:42

标签: angular typescript angular-material angular-cdk

我正在一个项目中,我需要显示1000条以上的记录,并考虑从有角度的CDK中使用虚拟滚动,但是由于某种原因,我遇到了这个错误:Error: CdkVirtualScrollViewport is already attached.

模板

<div class="news-feeds-wrapper">
  <div class="news-feeds-icon" *ngIf="configService.config.showNewsFeeds" (click)="toggleNewsFeeds()">
    <span *ngIf="platform.EDGE || platform.TRIDENT" class="icon-hype-notification_important"></span>
    <mat-icon *ngIf="!platform.EDGE && !platform.TRIDENT">notification_important</mat-icon>
  </div>
  <cdk-virtual-scroll-viewport itemSize="50">
    <div class="news-feeds-list" [ngClass]="{'open': newsFeedOpen}">
      <div *cdkVirtualFor="let group of newsFeeds">
        <div class="time" *ngIf="group.values.length > 0">{{group.type}}</div>
        <div class="news-feed" *cdkVirtualFor="let item of group.values | async">
          <div class="header">
            <i [ngSwitch]="item.action_type">
              <mat-icon *ngSwitchCase="'Task Assignment'">swap_horiz</mat-icon>
              <mat-icon *ngSwitchCase="'User Mention'">chat</mat-icon>
              <mat-icon *ngSwitchCase="'Task Deleted'">no_sim</mat-icon>
            </i>
            <span>{{item.action_type}}</span>
            <mat-icon class="deleted-news-feed" (click)="deletedNewsFeeds(group.values, item)">clear</mat-icon>
          </div>
          <div class="news-feed-content">
            <div class="info-content">
              <p class="project">{{item.project}}</p>
              <p class="taskboard">{{item.task_board}}</p>
              <p class="board-item">{{item.task_board_item}}</p>
            </div>
            <div class="avatar">
            </div>
          </div>
        </div>
      </div>
    </div>
  </cdk-virtual-scroll-viewport>
</div>

组件

@Component({
  selector: 'news-feeds',
  templateUrl: '../templates/news-feed.component.html',
  styleUrls: ['../../assets/scss/news-feeds.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class NewsFeedsComponent implements OnInit {
  public newsFeedOpen = false;
  public newsFeeds: Array<any> = [];

  @HostListener('document:click', ['$event'])
  closeNewsFeeds(event) {
    if (this.newsFeedOpen && event.target.localName != 'mat-icon') {
      this.newsFeedOpen = false;
    }
  }

  constructor(public platform: Platform, public configService: HypeConfigService, private cd: ChangeDetectorRef, private log: LoggingService, private backlogService: BacklogTaskService) {
    //
  }

  ngOnInit() {
    //
  }

  toggleNewsFeeds() {
    this.backlogService.getNewsFeeds().subscribe(
      (response) => {
        this.newsFeedOpen = !this.newsFeedOpen;
        this.newsFeeds = response;
        this.cd.markForCheck();
      },
      (error) => {
        this.log.error(`Error loading the news feeds: ${error}`);
      }
    );
  }

  deletedNewsFeeds(group: Array<any>, newsFeed: any) {
    const index = group.findIndex((i) => i.id === newsFeed.id);
    group.splice(index, 1);
  }
}

所以出于某种原因告诉我CdkVirtualScrollViewport已经连接,但是我没有在应用程序的任何其他地方使用它。 stackblitz

1 个答案:

答案 0 :(得分:1)

该问题是由于您使用* cdkVirtualFor两次,一个在另一个内部... 为解决此问题,我进行了2处更改;

  1. app.component.html :使用* ngFor代替* cdkVirtualFor,因此
    • Route::get('/', function () { return view('index'); }); Route::get('/{catchall?}', function () { return response()->view('index'); })->where('catchall', '(.*)'); 代替
    • <div class="news-feed" *ngFor="let item of group.values">
  2. app.component.css :添加了<div class="news-feed" *cdkVirtualFor="let item of group.values">,因为height is zero by default

下面的代码段中的HTML更改和CSS添加

cdk-virtual-scroll-viewport { height:400px; }
cdk-virtual-scroll-viewport {
  height: 400px;
}