How to Use Swiper Element with Angular 17?
Last Updated :
20 May, 2024
Using Swiper with Angular enhances user experience by providing touch-enabled sliders and carousels that are responsive and easy to navigate on mobile devices. It allows developers to create interactive and visually appealing UI components, enhancing the overall look and feel of the applications.
In this article, we will learn how can implement Swiper Element in our project.
Prerequisites
What are Swiper Elements?
Swiper is a modern, free, and open-source library that provides a flexible way to create touch-enabled, responsive sliders and carousels for websites and web applications. It offers features such as smooth transitions, support for various touch gestures, and customizable layouts for creating engaging user experiences on mobile devices.
Let us create a new angular 17 application and use swiper elements in it.
Approach
- Create Angular Application: We will create a new application using Angular CLI.
- Install Swiper Package: Then install the Swiper packages, a library provided by the Swiper Team.
- Register Custom Elements: Next, register the Swiper custom elements to ensure they are available in the Angular application.
- Integration with Angular Application: With Swiper installed and registered, we can start using Swiper elements.
Steps to Implement Swiper Element
Step 1: Create an angular application
We can make use of the angular cli to create a new application.
ng new swiper-elements
This command will create a new app in the directory swiper-elements, we then use cd command to change our active directory.
Step 2: Installing the swiper package
There is a library made by the Swiper team available at npm
npm install swiper
Folder Structure
Folder StructureDependencies
dependencies":
{
"@angular/animations": "^17.3.0",
"@angular/common": "^17.3.0",
"@angular/compiler": "^17.3.0",
"@angular/core": "^17.3.0",
"@angular/forms": "^17.3.0",
"@angular/platform-browser": "^17.3.0",
"@angular/platform-browser-dynamic": "^17.3.0",
"@angular/platform-server": "^17.3.0",
"@angular/router": "^17.3.0",
"@angular/ssr": "^17.3.7",
"express": "^4.18.2",
"rxjs": "~7.8.0",
"swiper": "^11.1.3",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
}
Step 3: Registering the custom element
We need to register the swiper elements, so that the custom elements are registered before the application tries to use them. We can do that by updating our main.ts file. This will make sure all the swiper elements that the application will try to use are available.
Example: To demonstrate using the swiper element in the Angular 17.
HTML
<!-- app.component.html -->
<div class="container">
<h1>Swiper Elements</h1>
<swiper-container>
<swiper-slide>Slide 1</swiper-slide>
<swiper-slide>Slide 2</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
<swiper-slide>Slide 4</swiper-slide>
</swiper-container>
</div>
CSS
/* app.component.scss */
.container {
max-width: 800px;
margin: 32px auto;
h1 {
text-align: center;
}
swiper-slide {
text-align: center;
font-size: 24px;
border-radius: 24px;
padding: 16px 0;
height: 400px;
&:nth-of-type(1) {
background-color: #780000;
color: white
}
&:nth-of-type(2) {
background-color: #fdf0d5;
}
&:nth-of-type(3) {
background-color: #003049;
color: white;
}
&:nth-of-type(4) {
background-color: #669bbc;
}
}
}
JavaScript
// app.component.ts
import { CUSTOM_ELEMENTS_SCHEMA, Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet],
templateUrl: './app.component.html',
styleUrl: './app.component.scss',
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class AppComponent {
title = 'swiper-elements';
}
JavaScript
// main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { register as registerSwiperElements } from 'swiper/element/bundle';
import { AppComponent } from './app/app.component';
registerSwiperElements();
bootstrapApplication(AppComponent, appConfig).catch((err) =>
console.error(err)
);
We just need to add CUSTOM_ELEMENTS_SCHEMA in the schemas array, since swiper is not an element known by angular template
Output
Swiper element in the Angular 17
Similar Reads
How to use Angular Material in Angular 17?
Angular Material is a comprehensive UI component library designed specifically for Angular applications. With its large collection of pre-built components and seamless integration with Angular, Angular Material simplifies the process of creating visually appealing and responsive user interfaces. In
2 min read
How to use angular-calendar in Angular 17?
Angular applications often require scheduling and event management features, which can be challenging to implement from scratch. Fortunately, Angular Calendar, a powerful library built specifically for Angular, provides the solution to this problem. In this article, we'll explore how Angular Calenda
2 min read
How to Scroll to an Element on click in Angular ?
In this article, we will see how to scroll to an element on click in Angular. Here, we will create a component that enables scrolling to specific targets when a button is pressed within the document from one target to another.Steps for Installing & Configuring the Angular ApplicationStep 1: Crea
4 min read
How To Use ViewChild in Angular?
In Angular, ViewChild is a powerful decorator that allows you to access and manipulate child components, directives, or DOM elements from a parent component. This feature is essential for scenarios where the parent needs to interact with its child components directly, such as invoking methods, acces
3 min read
How To Use PrimeNG Icon In Angular 17?
PrimeNG is a popular UI component library for Angular applications that provides a wide range of pre-built components, including icons. With Angular 17, PrimeNG icons can be easily integrated into your project. In this article, we'll walk you through how to set up and use PrimeNG icons in your Angul
2 min read
How to use Angular MatTabsModule with routing in Angular 17?
The Angular Material library provides a tab component called MatTabsModule that allows you to organize content into separate tabs. When combined with Angular routing, you can create a seamless navigation experience where each tab loads its content from a different route. In this article, we'll explo
4 min read
How to Navigate Fragment URL in Angular 15?
Fragment navigation is important in Angular applications for creating deep linking and bookmarking functionality within a single-page application (SPA). It allows users to navigate directly to specific sections or components within a page without reloading the entire page, enhancing user experience
3 min read
Use nested Components With Standalone Components in Angular 17
Angular 17 introduced a new feature called Standalone Components, which allows you to create self-contained components that can be used independently without being tied to a specific module. This feature can make your code more modular and easier to maintain. However, when working with Standalone Co
3 min read
How to Use the Async Pipe in Angular?
The AsyncPipe in Angular is a powerful and convenient tool used to handle asynchronous data streams such as observables and promises directly in the component template. It automatically subscribes to observables, renders their values, and updates the view when new data is emitted. This removes the n
3 min read
How to Use & Create custom directive in Angular?
Directives in Angular are nothing but the classes that allow us to add and modify the behavior of elements. Using directives in angular we can modify the DOM (Document Object Module) styles, handle user functionality, and much more. Table of Content Custom DirectivesSteps to create the DirectiveBene
3 min read