为什么 Swiper 在 angular 中对我不起作用? [英] Why is Swiper not working for me in angular?

查看:32
本文介绍了为什么 Swiper 在 angular 中对我不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

component.ts:

import 'swiper/swiper-bundle.css';导入 Swiper, { Navigation, Pagination } from 'swiper';@成分({选择器:'app-sing-accesorios',templateUrl: './sing-accesorios.component.html',styleUrls: ['./sing-accesorios.component.css']})导出类 SingAccesoriosComponent 实现 OnInit {构造函数(){Swiper.use([导航, 分页]);}ngOnInit(): 无效 {设置超时(功能(){var swiper = new Swiper('.swiper-container');},500)}}

component.html:

 <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>

component.css:

.swiper-container {宽度:100%;高度:100px;}.swiper-slide {文本对齐:居中;字体大小:18px;背景:#fff;/* 垂直居中滑动文本 */显示:-webkit-box;显示:-ms-flexbox;显示:-webkit-flex;显示:弹性;-webkit-box-pack: 中心;-ms-flex-pack:居中;-webkit-justify-content:中心;对齐内容:居中;-webkit-box-align:居中;-ms-flex-align:居中;-webkit-align-items:居中;对齐项目:居中;}

解决方案

参考本文档 Swiper Angular

在 HTML 上 - your.component.html

<ng-template swiperSlide>幻灯片1</ng-template><ng-template swiperSlide>幻灯片 2</ng-template><ng-template swiperSlide>幻灯片 3</ng-template></swiper>

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

@ViewChild('newSwiper') newSwiper: any;构造函数(){}ngOnInit(): 无效 {}ngAfterViewInit(): 无效 {控制台日志(this.newSwiper.swiperRef);//Swiper 实例将显示在控制台中}

现在你可以访问slideTo()、slideNext()、slidePrev()等属性和方法

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

希望这会有所帮助.

I am trying to use Swiper but I cannot achieve it, I followed the steps in the documentation (https://swiperjs.com/get-started/), the problem is that all the images / slides are mixed together, it allows me to slide to the side but since all the images are together it does not work. It shows me a slide with all the content, which I can slide but it has nothing on the sides. I'm doing it in ngOnlnit, at first not even the swiped worked correctly, but when I added setTimeout to delay the creation of var swiper it started to work (the swiped only)

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;
}

解决方案

Refer to this documentation Swiper Angular

on 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>

Create a reference to your swiper & You must instantiate swiper on AfterViewInit - your.component.ts

@ViewChild('newSwiper') newSwiper: any;

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

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

Now you can access properties and methods such as slideTo(), slideNext(), slidePrev() and etc.

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

Hope this will be helpful.

这篇关于为什么 Swiper 在 angular 中对我不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆