#Angular won't recognize SwiperJS HTML elements

75 messages · Page 1 of 1 (latest)

celest rain
#

No matter what I do, my Angular/Ionic project won't recognize SwiperJS HTML components.
I attached a screenshot that shows it.

I am on Angular 20, Ionic 8, Swiper 11.

My component.ts:

import {Component, CUSTOM_ELEMENTS_SCHEMA, inject} from '@angular/core';
import { register } from 'swiper/element/bundle';
import { Swiper } from 'swiper/types';

register();

But I can't do some of the properties from the SwiperJS doc here: https://swiperjs.com/element, because it doesn't recognize it as an HTML element. Scroll down to the Angular section.

what do I do?

slim sequoia
regal totem
#

Step 3 being the important one ☝️

short cradle
#

I had to ng cache clean for some reason, or I got the same error

celest rain
#

Unfortunately that stuff did not work

#

home.ts

schemas: [CUSTOM_ELEMENTS_SCHEMA]

main.ts

import { register as registerSwiperElements } from 'swiper/element/bundle';

registerSwiperElements();

if (environment.production) {
  enableProdMode();
}

bootstrapApplication(AppComponent, appConfig).catch(err => console.log(err));

#

I've removed node modules, removed package lock json, fresh install, cache clean. Doesn't matter I guess

regal totem
#

And you are attempting to use swiper-container in home.ts's template?

celest rain
#
 <swiper-container slides-per-view="1" space-between="10" centeredSlides="true"
                              loop="true" pagination="{clickable: true}"
                              effect="coverflow"    class="engageSwiper">
              @for (object of featuredEvents(); track object) {
                <swiper-slide (click)="navigateToEvents()">
                  <img ngSrc="{{object.imageUrl}}" class="custom" style="height: auto; width: auto" priority
                       width="1920" height="1080" alt="" ngSrcset="100w, 200w, 400w, 600w, 800w, 1200w, 1920w, 2560w">
                </swiper-slide>
              }
            </swiper-container>
#

Unknown html tag swiper-container

#

yes correct!

regal totem
#

Is this error reported by your IDE? Or is it also reported by ng serve / ng build?

celest rain
#

i would assume by ide.. ionic serve serves fine.

#

but I can't add certain attributes so i jsut attributed to an angular problem

regal totem
#

"Can't add" meaning what exactly?

celest rain
#

meaning when I do: [loop]="true" on the <swiper-container> it errors and doesn't like it (but still builds fine)

#

but that is shown what to do on the sswiper docs

regal totem
#

What do you mean by "it errors"?

celest rain
#

Unknown html tag swiper-container

regal totem
#

This reeks of an IDE problem.

celest rain
#

oops wrong one

#

Property loop is not provided by any applicable directives nor by <swiper-container> element

#

My intellij is relatively up to date. 2025.0.1

#

that's from the swiper docs, I should be able to do those on the element, but none of them show up

regal totem
#

So it also doesn't work?

celest rain
#

I know, that's what weird. I'm just trying to get it to recognize the HTML element, no matter what I do it doens't. But it still builds fine.

regal totem
#

This sounds like a bug with the IDE. I would try Invalidate Caches in your IDE

celest rain
#

I will try that!

celest rain
#

that didn't work, and so the issue for things like:

                                  centeredSlides: true
                                },
                                '1100': {
                                  slidesPerView: 3,
                                  centeredSlides: true
                                },
                                '2050': {
                                  slidesPerView: 3,
                                  centeredSlides: true,
                                  setWrapperSize: true
                                }
                              }

they don't work...

#

because it doesn't like that attribute

#

with the []

regal totem
#

"Don't work" meaning they show errors in your IDE?

#

Or they don't do what they should be doing?

celest rain
#

Yes, they show errors, but then also in the html they don't do what they should be doing

#

it recognizes the fields but in the html it like partially tries to render it but breaks other stuff

#

that's the html

#

again, I'm assuming this because it's not recognizing Swiper elements as html elements

regal totem
#

I don't know if Angular supports binding to properties of custom HTML elements.

celest rain
#

It's in the Swiper docs though?

slim sequoia
celest rain
celest rain
# slim sequoia no

right, that's kind of what I'm dealing with, is it's rendering some of the properties, but not all of them

slim sequoia
celest rain
#

I don't think it's an IDE thing. I just tried Webstorm and downloaded it and it still does not recognize swiper container

regal totem
#

"breakpoints" is a property, not an attribute. You will not see it in the DOM.

#

DOM attributes can only represent strings, breakpoints is an object.

celest rain
# regal totem What do you mean by "rendering"?

Meaning, when I serve the application, some of the properties that are on swiper container and actually working. When I try to implement properties doing [], it breaks some of the other properties for some reason.

regal totem
#

What do you mean by "break"?

#

Please be exact in your problem descriptions 😦

#

This works just fine...

#

Right click on "<swiper-container>", "use in console":

#

If this doesn't work, that's a Swiper issue. Angular is doing everything correctly.

#

So... this is a swiper issue and has nothing to do with Angular.

celest rain
#

I created a new angular project, added swiper, following that stack overflow article, and swiper container is still not recognized?

regal totem
#

You are talking about in your IDE?

celest rain
regal totem
#

Which ones have you tried?

celest rain
#

intellij ultimate, webstorm, and now visual studio code lol

regal totem
#

Well, looks like IDE support for this is just non-existent

celest rain
#

Yeah, this whole thing is pretty annoying. Didn't realize I would have these issues with it. Anyway, thank you so much for helping. it is greatly appreciated!

regal totem
#

Personally, the times I've used Swiper in Angular I have ditched their recommended way to use the custom element and just did it like you'd do in Vanilla JS with viewChild.

celest rain
#

Which example would that be?

celest rain
#

thank you will check that out!

celest rain
# regal totem <https://swiperjs.com/get-started#add-swiper-html-layout> <https://swiperjs.com/...

If you would be so kind, can I ask what I am missing here:

html:

 <div class="swiper engageSwiper">
              <!-- Additional required wrapper -->
              <div class="swiper-wrapper">
                <!-- Slides -->
                @for (object of featuredEvents(); track object) {
                  <div class="swiper-slide" (click)="navigateToEvents()">
                    <img ngSrc="{{object.imageUrl}}" class="custom" style="height: auto; width: auto" priority
                    width="1920" height="1080" alt="" ngSrcset="100w, 200w, 400w, 600w, 800w, 1200w, 1920w, 2560w">
                  </div>
                }
              </div>
              <!-- If we need pagination -->
              <div class="swiper-pagination"></div>

            </div>

.ts:

public swiper = new Swiper('.engageSwiper', {
    // configure Swiper to use modules
    modules: [Navigation, Pagination],
    spaceBetween: 10,
    pagination: {clickable: true},
    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev'
    },
    slidesPerView: 1,
    centeredSlides: true,
    loop: true,
    effect: "coverflow",
    autoplay: {
      delay: 2000,
      reverseDirection: true,
      disableOnInteraction: true
    },
    coverflowEffect: {
      rotate: 0,
      stretch: 0,
      depth: 100,
      modifier: 2.5
    },
    breakpoints: {
      700: {
        slidesPerView: 2,
        centeredSlides: true
      },
      1100: {
        slidesPerView: 3,
        centeredSlides: true
      },
      2050: {
        slidesPerView: 3,
        centeredSlides: true,
        setWrapperSize: true
      }
    }
  });

The swiper does show on my webpage, but the swiper object that I created in my .ts I'm assuming is not being connected to my HTML tag? I am not sure how you ddi the viewChild thing either as i was trying to do research on that.

#

The HTML swiper shows on my webpage, but none of those properties work because I'm assuming they aren't connected to that html element