Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nz-carousel not working in angular ssr project #9018

Open
gdayal80 opened this issue Feb 23, 2025 · 1 comment
Open

nz-carousel not working in angular ssr project #9018

gdayal80 opened this issue Feb 23, 2025 · 1 comment
Assignees

Comments

@gdayal80
Copy link

gdayal80 commented Feb 23, 2025

Reproduction link

(https://stackblitz.com/github/gdayal80/angular-ssr-nz-carousel?file=README.md)

Steps to reproduce

the project link given in *Link to minimal reproduction
the carousel shows only first slide on load, other slides are shown blank

What is expected?

the carousel should show the other slides similar to first slide

What is actually happening?

the carousel works in angular project without ssr, but does not work in angular project with ssr

Environment Info
ng-zorro-antd 17.4.1
Browser chrome Version 133.0.6943.126 (Official Build) (64-bit)
@yongshun950824
Copy link

yongshun950824 commented Mar 1, 2025

Currently, the carousel component doesn't support/check for SSR.

Refer to this commit, the Carousel component should be initialized and rendered only when it is running in the browser and after the hydration is completed.

For the temporary solution, I write the StackOverflow answer:

@if (isBrowser) {
  <nz-carousel nzAutoPlay>
    @for (index of array; track index) {
    <div nz-carousel-content>
      <h3>{{ index }}</h3>
    </div>
    }
  </nz-carousel>
}
import { AfterViewInit, Inject, PLATFORM_ID, ViewChild } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';

@ViewChild(NzCarouselComponent) carousel!: NzCarouselComponent;
isBrowser = false;

constructor(@Inject(PLATFORM_ID) private platformId: any) {
  this.isBrowser = isPlatformBrowser(this.platformId);
}

ngAfterViewInit() {
  if (this.isBrowser && this.carousel) {
    setTimeout(() => {
      this.carousel.layout();
    }, 0);
  }
}

Demo @ StackBlitz

Hope this helps to give the insight on fixing this bug.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants