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-ui-sortable實現(xiàn)可拖拽排序列表
這篇文章主要介紹了angular-ui-sortable實現(xiàn)可拖拽排序列表,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12AngularJS使用自定義指令替代ng-repeat的方法
這篇文章主要介紹了另一種即具有與ng-repeat一樣處理大量數(shù)據(jù)的綁定的功能,又具有超高性能的自定義方法,有需要的小伙伴們可以參考借鑒,下面來一起看看吧。2016-09-09解決angularjs中同步執(zhí)行http請求的方法
今天小編就為大家分享一篇解決angularjs中同步執(zhí)行http請求的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08AngularJS實現(xiàn)select的ng-options功能示例
這篇文章主要介紹了AngularJS實現(xiàn)select的ng-options功能,結合實例形式分析了AngularJS使用ng-options操作select列表的相關實現(xiàn)技巧,需要的朋友可以參考下2017-07-07