javascript - 为什么 Swiper 在 Angular 上不适合我?

标签 javascript angular typescript swiper.js

我正在尝试使用 Swiper 但我无法实现它,我按照文档中的步骤 (https://swiperjs.com/get-started/ ),问题是 所有图像/幻灯片都混合在一起,它允许我滑到一边,但由于所有图像都在一起,它不起作用。它向我展示了一张包含所有内容的幻灯片,我可以 滑动,但两侧没有任何东西。我在 ngOnlnit 中这样做,起初甚至刷卡都不能正常工作,但是当我添加 setTimeout 以延迟 var swiper 的创建时,它开始工作(仅限刷卡)

component.ts:

import 'swiper/swiper-bundle.css';
import Swiper, { Navigation, Pagination } from 'swiper';

@Component({
  selector: 'app-sing-accesorios',
  templateUrl: './sing-accesorios.component.html',
  styleUrls: ['./sing-accesorios.component.css']
})
export class SingAccesoriosComponent implements OnInit {

  constructor(){

    Swiper.use([Navigation, Pagination]);

  }

  ngOnInit(): void {

    setTimeout(function(){
        var swiper = new Swiper('.swiper-container');
    },500)

  }
}

component.html:

  <!-- Swiper -->
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide">Slide 1</div>
      <div class="swiper-slide">Slide 2</div>
      <div class="swiper-slide">Slide 3</div>
      <div class="swiper-slide">Slide 4</div>
      <div class="swiper-slide">Slide 5</div>
      <div class="swiper-slide">Slide 6</div>
      <div class="swiper-slide">Slide 7</div>
      <div class="swiper-slide">Slide 8</div>
      <div class="swiper-slide">Slide 9</div>
      <div class="swiper-slide">Slide 10</div>
    </div>
  </div>

component.css:

.swiper-container {
  width: 100%;
  height: 100px;
}

.swiper-slide {
  text-align: center;
  font-size: 18px;
  background: #fff;

  /* Center slide text vertically */
  display: -webkit-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
}

最佳答案

请参阅本文档 Swiper Angular

在 HTML 上 - your.component.html

<swiper [slidesPerView]="3" [spaceBetween]="50" (swiper)="onSwiper($event)" (slideChange)="onSlideChange()" #newSwiper>
  <ng-template swiperSlide>Slide 1</ng-template>
  <ng-template swiperSlide>Slide 2</ng-template>
  <ng-template swiperSlide>Slide 3</ng-template>
</swiper>

创建对您的 swiper 的引用 & 您必须在 AfterViewInit - your.component.ts 上实例化 swiper

@ViewChild('newSwiper') newSwiper: any;

  constructor() {
  }
  ngOnInit(): void {
  }

  ngAfterViewInit(): void {
    console.log(this.newSwiper.swiperRef);
    //Swiper instance will be displayed in console
  }

现在您可以访问属性和方法,例如 slideTo()、slideNext()、slidePrev() 等。

this.newSwiper.swiperRef.slideTo(2, 400);

希望这会有所帮助。

关于javascript - 为什么 Swiper 在 Angular 上不适合我?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63609797/

相关文章:

javascript - 重写此正则表达式而不使用 lookbehind - JS 的正则表达式无效

javascript - 如何为 Three.js 中的属性分配常量值

javascript - 在 Angular 2 组件中包含 SASS 变量

javascript - Angular 2 : How do you render HTML from a JSON response without displaying the tags to the user?

javascript - 为什么编译器在将数字/ bool 值分配给字符串 var 时不抛出错误

javascript - 使用 jquery $.ajax 到 PHP post GET 值

javascript - 发布的 github 页面上出现 "Failed to create temp file 18"错误

javascript - 滚动顶部在 angular2 中无法正常工作

angular - Typescript - 如何将错误响应转换为错误对象?

angular - 当组件更新值时,指令@Input 变量没有得到更新