请问一些 ng4.x里面引入轮播组件 无法执行下面的的命令,npm install @types/swiper --save
npm install @types/swiper --save-dev 应该怎么解决呢?
话不多说 ,反正网上的资料拿过来了跑项目,直接把环境装挂了,最终的罪人是无法翻墙,所以会报如下的错误:
然后各种找文章博客,都没用,都一样的告诉你他们就是这么装成功的。
推荐几篇我觉得很靠谱的博客地址:
1.https://blog.csdn.net/qishuixian/article/details/78798160(angular2_引入第三方文件之swiper的引入)
2.https://www.jianshu.com/p/98556affbf(Angular中使用Swiper)
3.http://www.imooc.com/article/73236
4.https://www.cnblogs.com/wangzhichao/p/7905251.html(# angular4(2-2)angular脚手架引入第三方类库(swiper)
)
5.https://blog.csdn.net/qq_39511525/article/details/80367334(这篇博客没看懂开头的那两个引入)
说了这么多,解决方案如下:
npm install -g cnpm --registry=https://registry.npm.taobao.org
其他人推荐的是淘宝镜像源:https://www.jianshu.com/p/fae87fef8ad0(参考这个吧,我就不搬了)
放一张我执行成功的图:
暂时先到这里,后面有问题再继续跟进,主要界面要写一个效果,想来还是加个插件来做:
可以参考如下效果图:
静态界面:
<div class="swiper-container">
<div class="swiper-wrapper" >
<div class="swiper-slide tl-color-0">
<div class="swiper-slide tl-card">
<div class="tl-font-30 tl-p-b6">40</div>
<div class="tl-font-14 tl-p-b6">一级会员人数</div>
<div class="tl-font-12 tl-p-b6"><span>充值:¥5000</span> <span>赠送:¥500</span></div>
<div class="tl-font-12"><span>充值:¥5000</span> <span>赠送:¥500</span></div>
</div>
</div>
<div class="swiper-slide tl-color-1">
<div class="swiper-slide tl-card">
<div class="tl-font-30 tl-p-b6">40</div>
<div class="tl-font-14 tl-p-b6">一级会员人数</div>
<div class="tl-font-12 tl-p-b6"><span>充值:¥5000</span> <span>赠送:¥500</span></div>
<div class="tl-font-12"><span>充值:¥5000</span> <span>赠送:¥500</span></div>
</div>
</div>
<div class="swiper-slide tl-color-2">
<div class="swiper-slide tl-card">
<div class="tl-font-30 tl-p-b6">40</div>
<div class="tl-font-14 tl-p-b6">一级会员人数</div>
<div class="tl-font-12 tl-p-b6"><span>充值:¥5000</span> <span>赠送:¥500</span></div>
<div class="tl-font-12"><span>充值:¥5000</span> <span>赠送:¥500</span></div>
</div>
</div>
<div class="swiper-slide tl-color-3">
<div class="swiper-slide tl-card">
<div class="tl-font-30 tl-p-b6">40</div>
<div class="tl-font-14 tl-p-b6">一级会员人数</div>
<div class="tl-font-12 tl-p-b6"><span>充值:¥5000</span> <span>赠送:¥500</span></div>
<div class="tl-font-12"><span>充值:¥5000</span> <span>赠送:¥500</span></div>
</div>
</div>
<div class="swiper-slide tl-color-4">
<div class="swiper-slide tl-card">
<div class="tl-font-30 tl-p-b6">40</div>
<div class="tl-font-14 tl-p-b6">一级会员人数</div>
<div class="tl-font-12 tl-p-b6"><span>充值:¥5000</span> <span>赠送:¥500</span></div>
<div class="tl-font-12"><span>充值:¥5000</span> <span>赠送:¥500</span></div>
</div>
</div>
<div class="swiper-slide tl-color-5">
<div class="swiper-slide tl-card">
<div class="tl-font-30 tl-p-b6">40</div>
<div class="tl-font-14 tl-p-b6">一级会员人数</div>
<div class="tl-font-12 tl-p-b6"><span>充值:¥5000</span> <span>赠送:¥500</span></div>
<div class="tl-font-12"><span>充值:¥5000</span> <span>赠送:¥500</span></div>
</div>
</div>
<div class="swiper-slide tl-color-6">
<div class="swiper-slide tl-card">
<div class="tl-font-30 tl-p-b6">40</div>
<div class="tl-font-14 tl-p-b6">一级会员人数</div>
<div class="tl-font-12 tl-p-b6"><span>充值:¥5000</span> <span>赠送:¥500</span></div>
<div class="tl-font-12"><span>充值:¥5000</span> <span>赠送:¥500</span></div>
</div>
</div>
<div class="swiper-slide tl-color-7">
<div class="swiper-slide tl-card">
<div class="tl-font-30 tl-p-b6">40</div>
<div class="tl-font-14 tl-p-b6">一级会员人数</div>
<div class="tl-font-12 tl-p-b6"><span>充值:¥5000</span> <span>赠送:¥500</span></div>
<div class="tl-font-12"><span>充值:¥5000</span> <span>赠送:¥500</span></div>
</div>
</div>
<!--<div class="swiper-slide tl-color-3">Slide 3</div>
<div class="swiper-slide tl-color-2">Slide 4</div>
<div class="swiper-slide tl-color-1">Slide 5</div>
<div class="swiper-slide tl-color-5">Slide 6</div>
<div class="swiper-slide tl-color-1">Slide 7</div>
<div class="swiper-slide tl-color-2">Slide 8</div>
<div class="swiper-slide tl-color-3">Slide 9</div>
<div class="swiper-slide tl-color-4">Slide 10</div>-->
</div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>
Css
.tl-card{
padding-left: 20px;
padding-right: 20px;
display: block!important;
}
.tl-card2{
width:210px;
height:123px;
border-radius:6px;
float: left;
margin-right: 17px;
padding-left: 20px;
padding-right: 20px;
}
.tl-color-0{
background: #FDAF94;
}
.tl-color-1{
background: #F7D570;
}
.tl-color-2{
background: #88BDF7;
}
.tl-color-3{
background: #A78BFA;
}
.tl-color-4{
background: #5EE7BB;
}
.tl-color-5{
background: #A3E96B;
}
.tl-color-6{
background: #DC8BFA;
}
.tl-color-7{
background: #FB8D8D;
}
.tl-font-30{
font-size:30px;
font-family:'PingFang-SC-Bold';
font-weight:bold;
color: #ffffff;
}
.tl-font-14{
font-size:14px;
font-family:'PingFang-SC-Bold';
font-weight:bold;
color: #ffffff;
}
.tl-font-12{
font-size:12px;
font-family:'PingFang-SC-Bold';
font-weight:bold;
color: #ffffff;
float: left;
}
.tl-p-b6{
margin-bottom: 6px;
display: flex;
}
/********* 这里来展示轮播图的效果******************************************/
.swiper-container {
width: 96%;
height: 120px;
margin: 20px;
padding-left: 20px;
}
.swiper-slide {
width: 250px!important;
height: 120px!important;
margin-right: 38px!important;
margin-left: 30px!important;
text-align: center;
border-radius:6px;
/*text-align: center;
font-size: 18px;
!* Center slide text vertically *!
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;*/
}
初始化函数,在顶部用这种引入全局变量的模式初始化Swiper,如果用import的模式不知为啥,new Swiper会报语法错误
参考一篇博客:# Angular4中使用后台去数据,Swiper不能滑动的解决方法
https://www.cnblogs.com/jack-lin/p/7195874.html
onloaded(){
var mySwiper = new Swiper('.swiper-container',{
slidesPerView : 3, //启用网格模式,每次滑动三个
slidesPerGroup : 3,
observer:true,//修改swiper自己或子元素时,自动初始化swiper (必须加)
observeParents:true,//修改swiper的父元素时,自动初始化swiper (必须加)
navigation: { //展示左右两侧的箭头图标
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
})
}
ngOnInit(): void {
console.log('Swiper',Swiper);
this.getMemberChartData('d');
// this.onloaded(); //如果在这里初始化会出现数据没有返回,就开始渲染的问题,导致swiper报错,如果是静态界面可以这里初始化
}
ngAfterViewInit(){
this.onloaded();
}
前提条件,你需要引入swiper插件:
angular4.x 如何正确的引入swiper插件https://www.jianshu.com/p/629e9908c1b3