I'm using ngx-slick-carousel to implement a slick carousel for the Angular app.
How to create the next and prev methods to switch slides from the component?
This is my html:
<ngx-slick-carousel class="carousel" #slickModal="slick-carousel" [config]="slideConfig" (init)="slickInit($event)" (breakpoint)="breakpoint($event)" (afterChange)="afterChange($event)" (beforeChange)="beforeChange($event)">
    <div ngxSlickItem *ngFor="let slide of slides" class="slide">
        <img [src]="slide.image" alt="" width="100%">
    </div>
</ngx-slick-carousel>
config:
slideConfig = {
    centerPadding: '60px',
    slidesToShow: 2,
    slidesToScroll: 1,
    arrows: false
};
Component:
import { SlickCarouselComponent } from 'ngx-slick-carousel';
@ViewChild('slickModal') slickModal: SlickCarouselComponent;
next() {
  this.slickModal.slickNext();
}
prev() {
  this.slickModal.slickPrev();
}
HTML:
<button class="btn-next" (click)="next()">next</button>
<button class="btn-prev" (click)="prev()">prev</button>
    import {Component, ViewChild} from '@angular/core';
    import { SlickCarouselComponent } from "ngx-slick-carousel";
    @Component({
        selector: 'slick-use-trackby-example',
        template: `
            <ngx-slick-carousel class="carousel"
                                #slickModal="slick-carousel"
                                [config]="slideConfig">
                <div ngxSlickItem *ngFor="let slide of slides; trackBy: _trackBy" class="slide">
                    <img src="{{ slide.img }}" alt="" width="100%">
                </div>
            </ngx-slick-carousel>
<button class="btn-next" (click)="next()">next</button>
<button class="btn-prev" (click)="prev()">prev</button>
    })
    export class SlickTrackbyExampleComponent {
        @ViewChild('slickModal', { static: true }) slickModal: SlickCarouselComponent;
        slides = [
            {img: 'http://placehold.it/350x150/000000'},
            {img: 'http://placehold.it/350x150/111111'},
            {img: 'http://placehold.it/350x150/333333'},
            {img: 'http://placehold.it/350x150/666666'}
        ];
      slideConfig = {
        slidesToShow: 4,
        slidesToScroll: 1,
        arrows: false,
        autoplay: false,
        autoplaySpeed: 3000,
        speed: 1500,
        responsive: [
          {
            breakpoint: 1024,
            settings: {
              slidesToShow: 3,
            }
          },
          {
            breakpoint: 600,
            settings: {
              slidesToShow: 2,
            }
          },
          {
            breakpoint: 480,
            settings: {
              slidesToShow: 1,
            }
          }
        ]
      };
      next() {
        this.slickModal.slickNext();
      }
      prev() {
        this.slickModal.slickPrev();
      }
    }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With