Angular 15 brings an exciting new feature: the Swiper 9 Element

I went through the swiperjs official documentation found at: swiperjs doc

To display images, I created a method to configure and initialize the swiper only when necessary.

Below is the HTML code snippet:

 <swiper-container #swiperRef init="false">
      <swiper-slide *ngFor="let src of srcPhotos">
          <img [src]="src" style="width: 100%; height: 100%; object-fit: cover;">
      </swiper-slide>
    </swiper-container>

And here's the corresponding TypeScript code:

_initSwiper() {
    const swiperEl: any = document.querySelector('swiper-container');

    const swiperParams = {
      slidesPerView: 1,
      breakpoints: {
        640: {
          slidesPerView: 2,
        },
        1024: {
          slidesPerView: 3,
        },
      },
      on: {
        init() {
          // ...
        },
      },
    };
    
    // Assign all parameters to Swiper element
    Object.assign(swiperEl, swiperParams);

    // Initialize the swiper
      swiperEl.initialize();

The swiper functions correctly, but an error message appears in the console every time:

console error

Specifically related to this line of code: swiperEl.initialize();

I'm struggling to identify the source of the problem.

Despite trying different initialization methods, the same error persists consistently.

Answer №1

When working with Angular, consider using ViewChild instead of querySelector for better efficiency. Simply add the following code snippet to the beginning of your class:

@ViewChild('swiperRef', { static: true })
protected _swiperRef: ElementRef;

Afterwards, replace this line:

const swiperEl: any = document.querySelector('swiper-container');

with the updated version:

const swiperEl = this._swiperRef.nativeElement;

Remember to ensure that _initSwiper() is only executed once srcPhotos contains valid values.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Evaluating string combinations in JavaScript using valid comparisons

After choosing values on the screen, two variables store their value. var uval = '100'; var eval = '5'; There are 2 combinations with values: let combination1= 'u:100;e:1,4,5,10' let combination2 = 'u:1000;e:120,400,500, ...

Utilize nodemailer in Angular 6 to effortlessly send emails

I am currently experiencing an issue with my email service form in my Angular 6 application integrated with Node.js. I have set up the form using Bootstrap and Nodemailer for sending emails, but it seems to not be working as expected. Whenever I attempt to ...

Guide on how to automatically navigate to the homepage upon logging in using Angular

It's puzzling to see my navbar appearing on the login page as well. Another issue is that even after successfully logging in and receiving the token, I am not redirected to the homepage. Let me outline what I have tried so far - starting with my modul ...

I'm having trouble getting my Ionic Angular App to start up properly. When I try to launch it, it just gets stuck on the splash screen and displays an error message saying "ReferenceError

The challenge I am facing involves developing an IOS app using ionic v7 and angular v15.2. While the app functions smoothly in the browser, upon deployment to my iPhone (iphone 13 pro, running IOS 15.6), I encounter a perplexing error message within XCode: ...

Troubleshooting a ForwardRef issue in a TypeScript and React Native project

Encountering a ts error while using forwardRef. // [ts] Property 'forwardRef' does not exist on type 'typeof React'. const MyComponent = React.forwardRef((props: Props, ref: any) => ... An issue arises in React Native when the pare ...

Is it possible for a component to accept another component as an input in Angular 2?

Important Reminder! Prior to implementing the technique mentioned in the answer, please review this discussion, which highlights the error Expression has changed after it was checked. Previous value: 'CD_INIT_VALUE'.. I have also shared a workar ...

Troubleshooting the accessibility issue between Docker node container and Angular/Node.js

After deciding to follow the angular tutorial provided on the angular website, I made the choice to download the "node" image from dockerhub. To ensure proper functionality, I carefully mapped container ports 4200 and 8080 to my physical Windows machine po ...

Collaborate on a component used in multiple modules

In my application, there are two modules: employer and landing. I have created a component in the landing module that I want to share with the employer module. To achieve this, I declared the component in the app.module.ts file of the parent module and use ...

Error: Unable to locate module with associated type definitions when utilizing Typescript in Next.js

Currently, I am working on a next.js project that I'm attempting to integrate typescript into. The structure of my folders is organized as follows: api aggregation.ts interfaces index.ts components Component1 index.js index.module.css ...

Tips for building an Angular app with Vite hosting

Looking to build an Angular application using Vite with simple routing, limited features, and no test files while running on port 5000. I have scoured the internet but haven't found a solution yet. ...

Display the dynamic change of minutes and seconds every second on an Ionic HTML page

I created a JavaScript countdown counter that displays minutes and seconds using the following line of code: document.getElementById("demo").innerHTML = Number(this.minutes) + 'm' + Number(this.seconds) + 's '; However, on iOS devices ...

Tips for effectively utilizing URLSearchParams in Angular 5 HttpClient's "get" function

When working with Angular 4.2, I used the Http service and made use of the get method by passing in a URLSearchParams object as the search parameter: this.http.get(url, {headers: this.setHeaders(), search: params}) Now I am looking to upgrade to Angular ...

How can we leverage RxJS combineLatest for observable filtering?

I'm exploring the possibility of utilizing combineLatest in an Angular service to eliminate the need for the activeFiler$ switch block (The service should achieve the same functionality). Currently, this is the structure of the component design (stack ...

What are the steps to showcase the content of a Typescript file on an HTML webpage?

We are looking to create a sample gallery with source code examples. Including typescript, html, and HTML files to showcase similar to the Angular.io component samples pages. Is there a way to extract the source code from a typescript file within our pro ...

Can angularJS unit tests be executed using the angular-cli command `ng test`?

In my current project, I am working on an AngularJS 1.6.X ES6 application and utilizing Mocha and Sinon for writing and running unit tests. Recently, I began the process of hybridizing/upgrading this application using ngUpgrade to incorporate new componen ...

Experience the magic of animated number counting using RxJs

I am working on my Angular application and I want to implement a feature where the records from HTTP get requests are animatedly counted. However, when using RxJs, the response is returned too quickly and I only see the final result immediately. I attemp ...

Error message thrown when attempting to access Navigator InjectionToken in tests: ReferenceError - Navigator is not defined

I have created an abstraction for the Navigator object: export const NAVIGATOR: InjectionToken<Navigator> = new InjectionToken<Navigator>( 'An abstraction over window.navigator object', { factory: () => inject(WINDOW).navig ...

What is the right way to configure an Axios Interceptor in a ReactJS app using TypeScript?

While I typically use JS with React, I decided to switch to Typescript. However, I've been encountering an error when trying to initialize a request interceptor instance: src/utils/services/axios.service.ts:16:8 TS2322: Type '{ 'Content-Type ...

Having trouble importing a TypeScript module from the global node_modules directory

I have a library folder located in the global node modules directory with a file named index.ts inside the library/src folder //inside index.ts export * from './components/button.component'; Now I am trying to import this into my angular-cli ap ...

Guide for creating a function that accepts an array containing multiple arrays as input

I am working with a function called drawSnake and it is being invoked in the following manner: drawSnake( [ [0, 0], [0, 1], [0, 2], [0, 3], [0, 4], ] ); How should I format the input for this function? I have attempted using Array<Array<[numb ...