首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ngx-swiper-wrapper已禁用

Ngx-swiper-wrapper已禁用
EN

Stack Overflow用户
提问于 2020-05-23 02:01:37
回答 1查看 1.1K关注 0票数 0

我已经将一些数据加载到我的ngx-swiper-wrapper中。然而,当发生这种情况时,这个禁用的快捷键被加载到next-button和prev-button上。我试着用javascript删除它,但是swiper对象仍然不能滚动。我不知道是否有办法在获得数据后刷新/重新加载swiper对象。

代码语言:javascript
复制
  ngOnInit() {
         this.spinner.show().then( async () => {
          this.data.currentProgress.subscribe(progress => this.progress = progress);
          await this.eventService.fetchEvents();
          }).then(async () => {
            this.data.totalShuffledEvents.subscribe(shuffle => this.totalEvents = shuffle);
            var buttons = document.querySelectorAll('.swiper-button-next');
            buttons[0].classList.remove("swiper-button-disabled");
          }).then(() => {
            this.spinner.hide();
          });
     }
代码语言:javascript
复制
          <swiper *ngIf="type == 'component' && show" class="swiper-container" fxFlex="auto" [config]="config" [disabled]="disabled" (indexChange)="onIndexChange($event)" (swiperTransitionStart)="onSwiperEvent('transitionStart')" (swiperTransitionEnd)="onSwiperEvent('transitionEnd')">
            <div *ngFor="let event of totalEvents" class="swiper-slide">
...
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-03 12:39:39

更好的解决方案是使用容器(父)来检索totalEvents,然后通过@Input将其传递给您的ngx-swiper-component。

下面是一个例子: changeDetection: ChangeDetectionStrategy.OnPush //非常重要

@ViewChild(SwiperDirective) swiperDirectiveRef: SwiperDirective;

代码语言:javascript
复制
ngOnChanges() {
    if (this.swiperDirectiveRef) {
      this.swiperDirectiveRef.setIndex(0);
      this.cdRef.detectChanges();
      if (this.swiperDirectiveRef.swiper()) {
        setTimeout(() => {
          this.swiperDirectiveRef.swiper().lazy.load();
        }, 0);
      }
    }
  }

或者:您可以为此创建一个方法,并在您的订阅中调用它:

代码语言:javascript
复制
ngOnInit() {
         this.spinner.show().then( async () => {
          this.data.currentProgress.subscribe(progress => this.progress = progress);
          await this.eventService.fetchEvents();
          }).then(async () => {
            this.data.totalShuffledEvents.subscribe(shuffle => {
            this.totalEvents = shuffle;
            this.refreshSwipper();
           });
          }).then(() => {
            this.spinner.hide();
          });
     }

refreshSwipper() {
if (this.swiperDirectiveRef) {
          this.swiperDirectiveRef.setIndex(0);
          this.cdRef.detectChanges();
          if (this.swiperDirectiveRef.swiper()) {
            setTimeout(() => {
              this.swiperDirectiveRef.swiper().lazy.load();
            }, 0);
          }
        }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61961141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档