Replies: 3 comments
-
|
Beta Was this translation helpful? Give feedback.
-
swiper.component.ts
main.ts (If you are using Angular SSR)
|
Beta Was this translation helpful? Give feedback.
-
Hello, I encounter the same issue using Swiper 11 with Angular 19 and SSR. My Setup :
Issue :When calling this.swiperElement.initialize() inside Steps to reproduce :
<swiper-container appSwiper [swiperOptions]="swiperOptions" init="false">
<swiper-slide>1</swiper-slide>
<swiper-slide>2</swiper-slide>
<swiper-slide>3</swiper-slide>
</swiper-container>
Workaround :I fixed the issue by ensuring import {AfterViewInit, Directive, ElementRef, inject, input, InputSignal, PLATFORM_ID} from '@angular/core';
import {SwiperContainer} from 'swiper/element';
import {SwiperOptions} from 'swiper/types';
import {isPlatformBrowser} from '@angular/common';
@Directive({
selector: '[appSwiper]'
})
export class SwiperDirective implements AfterViewInit {
readonly #platformId: Object = inject(PLATFORM_ID);
swiperElement: SwiperContainer = inject(ElementRef).nativeElement;
swiperOptions: InputSignal<SwiperOptions> = input.required<SwiperOptions>();
ngAfterViewInit(): void {
Object.assign(this.swiperElement, this.swiperOptions());
if (isPlatformBrowser(this.#platformId)) {
this.swiperElement.initialize();
}
}
} This prevent calling Question :Is there an official way to handle this issue in an SSR environment ? Or Should Swiper automatically check for SSR compatibility ? |
Beta Was this translation helpful? Give feedback.
-
Hi
I am getting this error in console, when I use Swiper in Angular 17 Server Side Rendering application.
ERROR TypeError: this.el.nativeElement.initialize is not a function.
This error occurs when I use directive for swiper
import { AfterViewInit, Directive, ElementRef, Input } from '@angular/core';
import { SwiperContainer } from 'swiper/element';
import { SwiperOptions } from 'swiper/types';
@directive({
selector: '[appSwiper]',
standalone: true
})
export class SwiperDirective implements AfterViewInit {
@input() config?: SwiperOptions;
constructor(private el: ElementRef) { }
ngAfterViewInit(): void {
Object.assign(this.el.nativeElement, this.config);
this.el.nativeElement.initialize();
}
}
Beta Was this translation helpful? Give feedback.
All reactions