angular - 在 Angular 11 中使用 Swiper JS 创建响应式幻灯片

标签 angular typescript swiper.js

我想在 Angular 11 中使用 swiper JS slide 断点,因此不同的屏幕尺寸断点将有不同的总 slidesPerView,我已经从 Swiper JS 阅读了这份文档 https://swiperjs.com/angular并从 https://www.npmjs.com/package/swiper 安装 swiper js

但是我不知道如何在组件ts文件中实现响应式断点。我已尝试在下面实现此代码,但 swiperConfig 无法正常工作。

这是HTML代码

 <swiper>
          <ng-template [swiperSlide]="swiperConfig"  *ngFor="let item of items">
            <div class="list-item">
                <app-item></app-item>
            </div>
          </ng-template>
 </swiper>

这是TS代码

import { Component, OnInit } from '@angular/core';
@Component({
  selector: 'app-swiper-test',
  templateUrl: './swiper-test.component.html',
  styleUrls: ['./swiper-test.component.scss']
})
export class SwiperTestComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }
  
  public swiperConfig = {
    slidesPerView: 'auto',
    spaceBetween: 20,
    breakpoints:{
      992:{
           slidesPerView: 4, 
          }
   }
  } 
}

谢谢

最佳答案

###更新

我已经解决了这个问题

这是html代码

<swiper [config]="swiperConfig">
    <ng-template swiperSlide *ngFor="let item of items">
        <div class="list-item">
            <app-item></app-item>
        </div>
    </ng-template>
</swiper>

这是TS代码

import { Component, OnInit } from '@angular/core';
import Swiper from 'swiper';

@Component({
  selector: 'app-swiper-test',
  templateUrl: './swiper-test.component.html',
  styleUrls: ['./swiper-test.component.scss']
})
export class SwiperTestComponent implements OnInit {
  
    swiperConfig: any = {
        slidesPerView: 'auto',
        spaceBetween: 20,
        breakpoints: {
            992: {
                spaceBetween: 20
            }
        }
    }

    constructor() {
    }

    ngOnInit() {
    }

}

希望对您有所帮助:)

关于angular - 在 Angular 11 中使用 Swiper JS 创建响应式幻灯片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67226179/

相关文章:

c# - 跨语言编码标准

node.js - 如何将静态 YAML 文件与 React Native 应用程序捆绑在一起?

javascript - iDangero.us Swiper 在 div 内设置隐藏显示时不起作用 :none

angular - 使用 ngx-swiper-wrapper 实现带有拇指的图像 slider

.net - (TTFB) 某些特定请求花费的时间比执行实际请求的时间长

angular - 如果值匹配,如何检查 Angular Material 复选框

meteor - TypeScript Javascript 模式

json - Angular2 http 获取 json 问题

angular - 如何仅在 primeNG 数据表 Angular 2 过滤器中的按键输入时触发

css - 如何在滑动器上覆盖透明图像并仍然能够滑动?