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   }

 

 

(本文原创,转载请注明出处!!)

 

转载于:https://www.cnblogs.com/yanliujun-tangxianjun/p/7655022.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值