在 Angular 8 中实现 Swiper

Implementing Swiper in Angular 8

我正在尝试在我的 Angular 8 应用程序中实施 Swiperhttps://swiperjs.com/get-started/

我在我的资产文件夹中创建了一个 javascript 文件并将其包含在我的 angular.json 中。

此外,我在 app.module.ts 和 运行 命令中包含了 Swiper npm install @types/swiper

但是,我得到错误:

[ts] Module '"../node_modules/@types/swiper/index has no exported member 'Swiper'

当它确实如此时。我不确定我哪里错了。

卡-swipe.component.ts

import { SwiperModule, SwiperConfigInterface } from 'ngx-swiper-wrapper';



@Component({
  selector: 'app-card-swipe',
  templateUrl: './card-swipe.component.html',
  styleUrls: ['./card-swipe.component.css']
})
export class CardSwipeComponent implements OnInit {


constructor() { }
something;
index;
config: SwiperConfigInterface = {
  a11y: true,
  direction: 'horizontal',
  slidesPerView: 3,
  slideToClickedSlide: true,
  mousewheel: true,
  scrollbar: false,
  watchSlidesProgress: true,
  navigation: true,
  keyboard: true,
  pagination: false,
  centeredSlides: true,
  loop: true,
  roundLengths: true,
  slidesOffsetBefore: 100,
  slidesOffsetAfter: 100,
  spaceBetween: 50,
  breakpoints: {
      // when window width is >= 320px
      320: {
          slidesPerView: 1
      }
  }
};

ngOnInit() {}

}

卡-swipe.component.html

<p>Hi this works</p>
<!-- Slider main container -->
<swiper fxFlex="auto" [config]="config" (indexChange)="onIndexChange($event)">
    <div *ngFor="let step of something; let index = index" class="swiper-slide cursor-pointer">
        <div fxLayout="column" fxLayoutAlign="center center" fxFlexFill class="mx-2">
          <p>hi</p>
          <h1>hello</h1>
        </div>
    </div>
</swiper>

我们在 Angular 应用程序中使用 Swiper。

我们如何整合它: Angular 有一个 npm 包 specificatlly: https://www.npmjs.com/package/ngx-swiper-wrapper

所以你基本上只需要安装包:

npm i ngx-swiper-wrapper

然后,导入模块(我们已将其放入 SharedModule 并导出,以便可以从任何地方访问它:

imports: [
    // more imports here
    SwiperModule
]

然后你可以像这样在你的组件中使用它:

<swiper fxFlex="auto" [config]="config" (indexChange)="onIndexChange($event)">
    <div *ngFor="let step of something; let index = index" class="swiper-slide cursor-pointer">
        <div fxLayout="column" fxLayoutAlign="center center" fxFlexFill class="mx-2">
            <!-- Your content goes here -->
        </div>
    </div>
</swiper>

作为一个配置,你可以有这样的东西:

    config: SwiperConfigInterface = {
        a11y: true,
        direction: 'horizontal',
        slidesPerView: 3,
        slideToClickedSlide: true,
        mousewheel: true,
        scrollbar: false,
        watchSlidesProgress: true,
        navigation: true,
        keyboard: true,
        pagination: false,
        centeredSlides: true,
        loop: true,
        roundLengths: true,
        slidesOffsetBefore: 100,
        slidesOffsetAfter: 100,
        spaceBetween: 50,
        breakpoints: {
            // when window width is >= 320px
            320: {
                slidesPerView: 1
            }
        }
    };

无需在 angular.json 文件中包含任何样式。它们都随您导入的模块一起提供。