代码之家  ›  专栏  ›  技术社区  ›  Miguel Frias

cdkVirtualScrollViewport已附加(错误)

  •  0
  • Miguel Frias  · 技术社区  · 6 年前

    我在一个项目中工作,我需要显示更多的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 回复  |  直到 6 年前
        1
  •  1
  •   Akber Iqbal    5 年前

    这个问题是因为你使用了*cdkVirtual两次,一次在另一次里面… 为了解决这个问题,我做了两个修改;

    1. app.component.html软件 :使用*ngfor而不是*cdkVirtualFor,因此
      • <div class="news-feed" *ngFor="let item of group.values"> 而不是
      • <div class="news-feed" *cdkVirtualFor="let item of group.values">
    2. app.component.css软件 :增加 cdk-virtual-scroll-viewport { height:400px; } 因为 height is zero by default

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

    cdk-virtual-scroll-viewport {
      height: 400px;
    }
    <div class="news-feeds-wrapper">
      <div class="news-feeds-list open">
        <cdk-virtual-scroll-viewport itemSize="50" class="example-viewport">
          <div *cdkVirtualFor="let group of newsfeeds">
            <div class="time">{{group.type}}</div>
            <div class="news-feed" *ngFor="let item of group.values">
              <div class="header">
                <span>{{item.action_type}}</span>
                <mat-icon class="deleted-news-feed">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>
        </cdk-virtual-scroll-viewport>
      </div>
    </div>