Jonathan
Jonathan

Reputation: 451

Implementing Swiper in Angular 8

I am trying to implement Swiper in my Angular 8 application: https://swiperjs.com/get-started/.

I have created a javascript file in my assets folder and included it into my angular.json.

Also I have included Swiper in my app.module.ts and ran the command npm install @types/swiper.

However, I get the error:

[ts] Module '"../node_modules/@types/swiper/index has no exported member 'Swiper'

when it clearly does. I am not sure where I am going wrong.

card-swipe.component.ts

import { SwiperModule, SwiperConfigInterface } from 'ngx-swiper-wrapper';



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


constructor() { }
something;
index;
config: SwiperConfigInterface = {
  a11y: true,
  direction: 'horizontal',
  slidesPerView: 3,
  slideToClickedSlide: true,
  mousewheel: true,
  scrollbar: false,
  watchSlidesProgress: true,
  navigation: true,
  keyboard: true,
  pagination: false,
  centeredSlides: true,
  loop: true,
  roundLengths: true,
  slidesOffsetBefore: 100,
  slidesOffsetAfter: 100,
  spaceBetween: 50,
  breakpoints: {
      // when window width is >= 320px
      320: {
          slidesPerView: 1
      }
  }
};

ngOnInit() {}

}

card-swipe.component.html

<p>Hi this works</p>
<!-- Slider main container -->
<swiper fxFlex="auto" [config]="config" (indexChange)="onIndexChange($event)">
    <div *ngFor="let step of something; let index = index" class="swiper-slide cursor-pointer">
        <div fxLayout="column" fxLayoutAlign="center center" fxFlexFill class="mx-2">
          <p>hi</p>
          <h1>hello</h1>
        </div>
    </div>
</swiper>

Upvotes: 2

Views: 23316

Answers (1)

dave0688
dave0688

Reputation: 5790

We're using Swiper in our Angular application.

How we integrated it: There's an npm package specificatlly for Angular: https://www.npmjs.com/package/ngx-swiper-wrapper

So you basically just have to install the package:

npm i ngx-swiper-wrapper

Then, import the module (we've put that into our SharedModule and exported it, so that it's accessable from everywhere:

imports: [
    // more imports here
    SwiperModule
]

and then you can use it in your components like this:

<swiper fxFlex="auto" [config]="config" (indexChange)="onIndexChange($event)">
    <div *ngFor="let step of something; let index = index" class="swiper-slide cursor-pointer">
        <div fxLayout="column" fxLayoutAlign="center center" fxFlexFill class="mx-2">
            <!-- Your content goes here -->
        </div>
    </div>
</swiper>

As a config you can have something like this:

    config: SwiperConfigInterface = {
        a11y: true,
        direction: 'horizontal',
        slidesPerView: 3,
        slideToClickedSlide: true,
        mousewheel: true,
        scrollbar: false,
        watchSlidesProgress: true,
        navigation: true,
        keyboard: true,
        pagination: false,
        centeredSlides: true,
        loop: true,
        roundLengths: true,
        slidesOffsetBefore: 100,
        slidesOffsetAfter: 100,
        spaceBetween: 50,
        breakpoints: {
            // when window width is >= 320px
            320: {
                slidesPerView: 1
            }
        }
    };

No need to include any styles in your angular.json file. They all come with that module you imported.

Upvotes: 7

Related Questions