angula2 輪播效果組件angular2-swiper


github地址:https://github.com/ksachdeva/angular2-swiper

 

 一、安裝

npm install swiper angular2-swiper --save-dev

 

二、配置

1、angular-cli.json

"styles": [
     "../node_modules/swiper/dist/css/swiper.css"
],

按理說這樣寫是可以獲取到樣式的,但是在我本地的項目死活獲取不到,我就直接把樣式文件拷貝到assets文件夾下,然后在index.html里面直接引用<link rel="stylesheet" href="/assets/css/swiper.css">

2、模塊module.ts文件

import {KSSwiperModule} from "angular2-swiper";
 
imports: [
    KSSwiperModule
  ],

 重新運行cnpm start的時候,可能會報錯:ERROR in KKSwiperModule is not an Ngmodule;跟着錯誤來到ks-swiper.module.d.ts文件,給他添加一個ngModule如下即可:

import { NgModule } from "@angular/core";
@NgModule() export declare class KSSwiperModule { }

 

三、使用

1、html文件

 1   <div  class="top-box" *ngIf="dataFlag == 'true'">
 2     <div class="top-list" > 
 3       <ks-swiper-container [options]="swipeOptions">
 4         <ks-swiper-slide *ngFor="let rowData of tradeTop" class="top-book"> 
 5             <img src="{{rowData.goods_cover_path}}">
 6             <span>已售{{rowData.total_count}}本</span> 
 7             <p>{{rowData.goods_name}}</p> 
 8         </ks-swiper-slide>
 9       </ks-swiper-container>
10     </div>
11   </div> 
12   <button class="left-active" [class.show]="preNextBtnFlag == 'true'"> </button>
13   <button class="right-active"  [class.show]="preNextBtnFlag == 'true'"> </button> 

2、組件component.ts文件

 1 import { KSSwiperContainer, KSSwiperSlide } from 'angular2-swiper';
 2 
 3 const pageLinkSize = 6;
 4 
 5 @Component({
 6   selector: 'tl-top',
 7   templateUrl: './top.component.html',
 8   styleUrls: ['./top.component.css']
 9 })
10 export class TopComponent implements OnInit {
11 
12   @Input() tradeTop;
13 
14   public preNextBtnFlag: string = 'false';
15   public dataFlag: string = 'false';
16   public swipeOptions: any;
17 
18   constructor() { }
19 
20   ngOnInit() {
21     this.swiperFn();
22   }
23   
24   ngOnChanges(changes) {
25     let tradeTopArr = changes['tradeTop'] && changes['tradeTop']['currentValue'];  // tradeTop為后台獲取的數據 26     if (tradeTopArr) {
27       let tradeTopArrLen = tradeTopArr.length;
28       this.dataFlag = 'true'; 
29       this.swiperFn();
30       if ( tradeTopArrLen < 7) { 
31         this.preNextBtnFlag = 'false';
32       } else if (tradeTopArrLen >= 7) {
33         this.preNextBtnFlag = 'true'; 
34       }
35     }
36   }
37 
38   // 輪播配置
39   swiperFn() {
40     this.swipeOptions = {
41       slidesPerView: 6,
42       nextButton: '.right-active',
43       prevButton: '.left-active',
44       spaceBetween: 40
45     };
46   }

 

 

(本文原創,轉載請注明出處!!)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM