'Why is Swiper not working for me in angular?

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


Solution 1:[1]

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.

Solution 2:[2]

In my case, adding

.swiper-wrapper{
    display:inline-flex;
}

works as intended.

Solution 3:[3]

For anyone having this problem in future - instead of using setTimeout just move the Swiper initialization from OnInit to AfterViewInit, then it works like a charm. Also there's no need to use any 3rd party libraries, as Swiper already natively supports Angular 2+.

Additionally please make sure you include Swiper CSS styles (at minimum core styles or entire bundle if you like).

Solution 4:[4]

setTimeout(function () {
  const swiper = new Swiper('.swiper-container', {
    autoplay: {
      reverseDirection: true,
    },
  });
  const mySwiper = new Swiper(".swiper-container", {
    autoplay: true,
  });
  const swiper1 = new Swiper('.swiper-container2', {
    autoplay: {
      reverseDirection: true,
    },
  });
}, 4000);

its works for me

Solution 5:[5]

make sure you include Swiper CSS styles and also use encapsulation in @Component

@Component({
  selector: 'app-sing-accesorios',
  templateUrl: './sing-accesorios.component.html',
  styleUrls: ['./sing-accesorios.component.css'],
  encapsulation: ViewEncapsulation.None
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shazeen Siraj
Solution 2 Hanho Kim
Solution 3 MRSoft
Solution 4 hamed.nosrati
Solution 5 Amin MilanTash