@@ -6,10 +6,12 @@ import {
6
6
ElementRef ,
7
7
EventEmitter ,
8
8
HostBinding ,
9
+ Inject ,
9
10
Input ,
10
11
NgZone ,
11
12
OnInit ,
12
13
Output ,
14
+ PLATFORM_ID ,
13
15
QueryList ,
14
16
SimpleChanges ,
15
17
ViewChild ,
@@ -28,6 +30,7 @@ import {
28
30
ScrollbarOptions ,
29
31
VirtualOptions ,
30
32
} from 'swiper/types' ;
33
+ import { isPlatformBrowser } from '@angular/common' ;
31
34
@Component ( {
32
35
selector : 'swiper, [swiper]' ,
33
36
templateUrl : './swiper.component.html' ,
@@ -415,9 +418,10 @@ export class SwiperComponent implements OnInit {
415
418
416
419
@HostBinding ( 'class' ) containerClasses = 'swiper-container' ;
417
420
constructor (
418
- private zone : NgZone ,
421
+ private _ngZone : NgZone ,
419
422
private elementRef : ElementRef ,
420
423
private _changeDetectorRef : ChangeDetectorRef ,
424
+ @Inject ( PLATFORM_ID ) private _platformId ,
421
425
) { }
422
426
423
427
private _setElement ( el : ElementRef , ref : any , update : string , key = 'el' ) {
@@ -441,11 +445,8 @@ export class SwiperComponent implements OnInit {
441
445
442
446
ngAfterViewInit ( ) {
443
447
this . childrenSlidesInit ( ) ;
444
-
445
- if ( this . init ) {
446
- this . initSwiper ( ) ;
447
- this . _changeDetectorRef . detectChanges ( ) ;
448
- }
448
+ this . initSwiper ( ) ;
449
+ this . _changeDetectorRef . detectChanges ( ) ;
449
450
}
450
451
451
452
private childrenSlidesInit ( ) {
@@ -476,65 +477,65 @@ export class SwiperComponent implements OnInit {
476
477
initSwiper ( ) {
477
478
const { params : swiperParams , passedParams } = getParams ( this ) ;
478
479
Object . assign ( this , swiperParams ) ;
479
- swiperParams . onAny = ( event , ...args ) => {
480
- const emitter = this [ `s_${ event } ` ] as EventEmitter < any > ;
481
- if ( emitter ) {
482
- emitter . emit ( ...args ) ;
483
- }
484
- } ;
485
-
486
- Object . assign ( swiperParams . on , {
487
- slideChange : ( ) => {
488
- this . indexChange . emit ( this . swiperRef . realIndex ) ;
489
- } ,
490
- _containerClasses ( swiper , classes ) {
491
- this . containerClasses = classes ;
492
- } ,
493
- _swiper : ( swiper ) => {
494
- this . swiperRef = swiper ;
495
- this . s_swiper . emit ( this . swiperRef ) ;
496
- swiper . loopCreate = ( ) => { } ;
497
- swiper . loopDestroy = ( ) => { } ;
498
- if ( swiperParams . loop ) {
499
- swiper . loopedSlides = this . loopedSlides ;
480
+ this . _ngZone . runOutsideAngular ( ( ) => {
481
+ swiperParams . init = false ;
482
+ swiperParams . observer = true ;
483
+ swiperParams . onAny = ( event , ...args ) => {
484
+ const emitter = this [ `s_${ event } ` ] as EventEmitter < any > ;
485
+ if ( emitter ) {
486
+ emitter . emit ( ...args ) ;
500
487
}
501
- if ( swiper . virtual && swiper . params . virtual . enabled ) {
502
- swiper . virtual . slides = this . slides ;
503
- const extendWith = {
504
- cache : false ,
505
- renderExternal : ( data ) => {
506
- this . updateVirtualSlides ( data ) ;
507
- } ,
508
- renderExternalUpdate : false ,
509
- } ;
510
- extend ( swiper . params . virtual , extendWith ) ;
511
- extend ( swiper . originalParams . virtual , extendWith ) ;
512
- }
513
- this . _changeDetectorRef . detectChanges ( ) ;
514
- } ,
515
- _slideClasses : ( _ , updated ) => {
516
- updated . forEach ( ( { slideEl, classNames } , index ) => {
517
- const slideIndex = parseInt ( slideEl . getAttribute ( 'data-swiper-slide-index' ) ) || index ;
518
- if ( this . virtual ) {
519
- const virtualSlide = this . slides . find ( ( item ) => {
520
- return item . virtualIndex && item . virtualIndex === slideIndex ;
521
- } ) ;
522
- if ( virtualSlide ) {
523
- virtualSlide . classNames = classNames ;
524
- return ;
488
+ } ;
489
+ Object . assign ( swiperParams . on , {
490
+ _containerClasses ( swiper , classes ) {
491
+ this . containerClasses = classes ;
492
+ } ,
493
+ _slideClasses : ( _ , updated ) => {
494
+ updated . forEach ( ( { slideEl, classNames } , index ) => {
495
+ const slideIndex = parseInt ( slideEl . getAttribute ( 'data-swiper-slide-index' ) ) || index ;
496
+ if ( this . virtual ) {
497
+ const virtualSlide = this . slides . find ( ( item ) => {
498
+ return item . virtualIndex && item . virtualIndex === slideIndex ;
499
+ } ) ;
500
+ if ( virtualSlide ) {
501
+ virtualSlide . classNames = classNames ;
502
+ return ;
503
+ }
525
504
}
526
- }
527
505
528
- if ( this . slides [ slideIndex ] ) {
529
- this . slides [ slideIndex ] . classNames = classNames ;
530
- }
531
- } ) ;
506
+ if ( this . slides [ slideIndex ] ) {
507
+ this . slides [ slideIndex ] . classNames = classNames ;
508
+ }
509
+ } ) ;
510
+ this . _changeDetectorRef . detectChanges ( ) ;
511
+ } ,
512
+ } ) ;
513
+ const swiperRef = new Swiper ( swiperParams ) ;
514
+ swiperRef . loopCreate = ( ) => { } ;
515
+ swiperRef . loopDestroy = ( ) => { } ;
516
+ if ( swiperParams . loop ) {
517
+ swiperRef . loopedSlides = this . loopedSlides ;
518
+ }
519
+ if ( swiperRef . virtual && swiperRef . params . virtual . enabled ) {
520
+ swiperRef . virtual . slides = this . slides ;
521
+ const extendWith = {
522
+ cache : false ,
523
+ renderExternal : ( data ) => {
524
+ this . updateVirtualSlides ( data ) ;
525
+ } ,
526
+ renderExternalUpdate : false ,
527
+ } ;
528
+ extend ( swiperRef . params . virtual , extendWith ) ;
529
+ extend ( swiperRef . originalParams . virtual , extendWith ) ;
530
+ }
531
+
532
+ if ( isPlatformBrowser ( this . _platformId ) ) {
533
+ this . swiperRef = swiperRef . init ( this . elementRef . nativeElement ) ;
532
534
this . _changeDetectorRef . detectChanges ( ) ;
533
- } ,
534
- } ) ;
535
- swiperParams . observer = true ;
536
- this . zone . runOutsideAngular ( ( ) => {
537
- new Swiper ( this . elementRef . nativeElement , swiperParams ) ;
535
+ swiperRef . on ( 'slideChange' , ( ) => {
536
+ this . indexChange . emit ( this . swiperRef . realIndex ) ;
537
+ } ) ;
538
+ }
538
539
} ) ;
539
540
}
540
541
@@ -561,7 +562,7 @@ export class SwiperComponent implements OnInit {
561
562
this . currentVirtualData = virtualData ;
562
563
this . _activeSlides . next ( virtualData . slides ) ;
563
564
this . _changeDetectorRef . detectChanges ( ) ;
564
- this . zone . runOutsideAngular ( ( ) => {
565
+ this . _ngZone . runOutsideAngular ( ( ) => {
565
566
this . swiperRef . updateSlides ( ) ;
566
567
this . swiperRef . updateProgress ( ) ;
567
568
this . swiperRef . updateSlidesClasses ( ) ;
@@ -583,7 +584,7 @@ export class SwiperComponent implements OnInit {
583
584
return ;
584
585
}
585
586
586
- this . zone . runOutsideAngular ( ( ) => {
587
+ this . _ngZone . runOutsideAngular ( ( ) => {
587
588
const {
588
589
params : currentParams ,
589
590
pagination,
@@ -663,7 +664,7 @@ export class SwiperComponent implements OnInit {
663
664
}
664
665
665
666
updateSwiper ( changedParams : SimpleChanges | any ) {
666
- this . zone . runOutsideAngular ( ( ) => {
667
+ this . _ngZone . runOutsideAngular ( ( ) => {
667
668
if ( changedParams . config ) {
668
669
return ;
669
670
}
@@ -749,7 +750,7 @@ export class SwiperComponent implements OnInit {
749
750
if ( index === this . swiperRef . activeIndex ) {
750
751
return ;
751
752
}
752
- this . zone . runOutsideAngular ( ( ) => {
753
+ this . _ngZone . runOutsideAngular ( ( ) => {
753
754
if ( this . loop ) {
754
755
this . swiperRef . slideToLoop ( index , speed , ! silent ) ;
755
756
} else {
@@ -759,7 +760,7 @@ export class SwiperComponent implements OnInit {
759
760
}
760
761
761
762
ngOnDestroy ( ) {
762
- this . zone . runOutsideAngular ( ( ) => {
763
+ this . _ngZone . runOutsideAngular ( ( ) => {
763
764
this . swiperRef ?. destroy ( ) ;
764
765
} ) ;
765
766
}
0 commit comments