Slick Carousel Tutorial Example Angular 11/12

Slick Carousel Tutorial Example Angular 11/12. In this tutorial, we will explain how to use Slick Carousel in the Angular 11/12 app using the ngx-slick-carousel-o package.

This Angular tutorial is compatible with version 4+ including latest version 12, 11, 10, 9, 8 ,7, 6 & 5.

Slick Carousel Tutorial Example Angular 11/12

We simply need to follow the given steps for implementing the slick carousel in the Angular 12 app:

 

Step 1 is to Create the New Angular App

Step 2 is to Install the Slick Carousel Library

Step 3 is to Add Code on App.Module.ts File

Step 4 is to Add Code on View File

Step 5 is to Add Code On app.Component ts File

Step 6 is to Start the Angular App

 

Now, Let’s Learn in Detail

Step 1- Creating the New Angular App

First of all, open your terminal and execute the given command on it to install an angular app:

ng new slick-carousel-app

Step 2 : Installing the Slick Carousel Library

Next, just install the NPM package ngx-slick-carousel -save for implementing the slick carousel in the Angular app. Further, we can install the packages by executing the below-mentioned commands on the terminal:

npm install jquery --save
npm install slick-carousel --save
npm install ngx-slick-carousel --save

Then, we have to open the angular.json file and update the below code into it:

...
"styles": [
  "node_modules/slick-carousel/slick/slick.scss",
  "node_modules/slick-carousel/slick/slick-theme.scss"
],
"scripts": [
  "node_modules/jquery/dist/jquery.min.js",
  "node_modules/slick-carousel/slick/slick.min.js"
]
....

Step 3: Adding Code on App.Module.ts File

Afterward, we visit the src/app directory and open the app.module.ts file. Thereafter, we also add the given lines into the app.module.ts file:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
   
import { AppComponent } from './app.component';
import { SlickCarouselModule } from 'ngx-slick-carousel';
   
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    SlickCarouselModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Step 4: Adding the Code on View File

In the next step, create the slick carousel in the Angular app. Then, we visit src/app/ and app.component.html after which, we update the following code into it:

<h1>Angular Slick Carousel/Slider Integration Tutorial - Thecodemon.com</h1>
   
<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.img }}" alt="" width="100%">
        </div>
</ngx-slick-carousel>

Step 5: Adding Code On app.Component ts File

Now, follow this step in which we visit the src/app directory and open app.component.ts.  After this, we add the following code into component.ts file:

import { Component } from '@angular/core';
   
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'ng-carousel-demo';
   
  slides = [
    {img: "https://dummyimage.com/350x150/423b42/fff"},
    {img: "https://dummyimage.com/350x150/2a2b7a/fff"},
    {img: "https://dummyimage.com/350x150/1a2b7a/fff"},
    {img: "https://dummyimage.com/350x150/7a2b7a/fff"},
    {img: "https://dummyimage.com/350x150/9a2b7a/fff"},
    {img: "https://dummyimage.com/350x150/5a2b7a/fff"},
    {img: "https://dummyimage.com/350x150/4a2b7a/fff"}
  ];
  slideConfig = {"slidesToShow": 4, "slidesToScroll": 4};
   
  slickInit(e) {
    console.log('slick initialized');
  }
     
  breakpoint(e) {
    console.log('breakpoint');
  }
     
  afterChange(e) {
    console.log('afterChange');
  }
     
  beforeChange(e) {
    console.log('beforeChange');
  }
}

Step 6: Starting the Angular App

Lastly, we reach the final step of implementation where we execute the following command on the terminal to start the slick carousel in the angular app:

ng serve

Conclusion

Finally, we reach the end of using the slick carousel in the Angular 11/12 tutorial. In this tutorial, we have given a simple example with the help of which you can easily use the slick carousel yourself in your Angular app. 

Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *