Angular6中使用Swiper的方法示例

 更新时间:2018年07月09日 14:20:56   作者:小略子  
这篇文章主要介绍了Angular6中使用Swiper的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

项目使用的Angular版本是V6.0.3


安装Swiper

npm install swiper --save

或者

yarn add swiper --save

在angular.json文件添加swiper.js和swiper.css

angular.json

安装模组定义档

npm install @types/swiper --save

或者

yarn add @types/swiper --save

配置tsconfig文件

tsconfig.json

tsconfig.app.json

按照上面的配置完成后,angular里就可以用swiper。下面是一个小demo。

test.component.html

<div class="swiper-container">
  <div class="swiper-wrapper">
    <div class="swiper-slide" *ngFor="let data of slides">
      <img [src]="data" alt="" width="100%">
    </div>
  </div>
  <!-- 如果需要分页器 -->
  <div class="swiper-pagination"></div>

  <!-- 如果需要导航按钮 -->
  <div class="swiper-button-prev"></div>
  <div class="swiper-button-next"></div>
</div>

test.component.ts

import {
  AfterViewInit,
  Component,
  OnInit
} from '@angular/core';

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html'
})
export class TestComponent implements AfterViewInit {
  testSwiper: Swiper;
  slides = [
    'https://via.placeholder.com/300x200/FF5733/ffffff',
    'https://via.placeholder.com/300x200/C70039/ffffff',
    'https://via.placeholder.com/300x200/900C3F/ffffff'
  ];

  constructor() {}

  ngAfterViewInit() {
    this.testSwiper = new Swiper('.swiper-container', {
      direction: 'horizontal',
      loop: true,
      // 如果需要分页器
      pagination: {
        el: '.swiper-pagination',
      },
      // 如果需要前进后退按钮
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
      // 如果需要滚动条
      scrollbar: {
        el: '.swiper-scrollbar',
      },
    });
  }
}

运行结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • angular中如何绑定iframe中src的方法

    angular中如何绑定iframe中src的方法

    这篇文章主要介绍了angular中如何绑定iframe中src的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-02-02
  • 使用angular-cli webpack创建多个包的方法

    使用angular-cli webpack创建多个包的方法

    这篇文章主要介绍了使用angular-cli webpack创建多个包的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-10-10
  • Angular 如何使用第三方库的方法

    Angular 如何使用第三方库的方法

    本篇文章主要介绍了Angular 如何使用第三方库的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • 发布Angular应用至生产环境的方法

    发布Angular应用至生产环境的方法

    这篇文章主要介绍了发布Angular应用至生产环境的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • AngularJs用户输入动态模板XSS攻击示例详解

    AngularJs用户输入动态模板XSS攻击示例详解

    这篇文章主要给大家介绍了关于AngularJs用户输入动态模板XSS攻击的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用angularjs具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-04-04
  • angular6 Error:Debug Failure at typeToString解决分析

    angular6 Error:Debug Failure at typeToString解决分析

    这篇文章主要为大家介绍了angular6 Error:Debug Failure at typeToString解决分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • AngularJS表单验证功能

    AngularJS表单验证功能

    这篇文章主要为大家详细介绍了AngularJS表单验证功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Angular中封装fancyBox(图片预览)遇到问题小结

    Angular中封装fancyBox(图片预览)遇到问题小结

    这篇文章主要介绍了Angular中封装fancyBox(图片预览)遇到的问题小结,需要的朋友可以参考下
    2017-09-09
  • angular.js之路由的选择方法

    angular.js之路由的选择方法

    下面小编就为大家带来一篇angular.js之路由的选择方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • Angular自定义组件实现数据双向数据绑定的实例

    Angular自定义组件实现数据双向数据绑定的实例

    下面小编就为大家分享一篇Angular自定义组件实现数据双向数据绑定的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12

最新评论