时间:2022-12-06 21:30
微信小程序动态更新页面的案例:
wxml代码:
<swiperindicator-dots="{{true}}"current="{{currentindex}}">
<swiper-itemwx:for="{{novel}}"wx:key="key">
<viewclass="containercard">
<imagesrc="{{item.imagePath}}"></image>
<text>第{{index+1}}部:{{item.name}}</text>
<text>评价:{{item.comment}}</text>
<textbindtap="onLoad"wx:if="{{index<novel.length-1}}">返回尾页</text>
</view>
</swiper-item>
</swiper>
<buttonbindtap="f1">更新书籍</button>
</view>
wxss代码:
.container1{height:100vh;
display:flex;
flex-direction:column;
justify-content:space-around;
align-items:center;
}
.novel{
display:flex;
}
.novel-image{
width:200rpx;
height:200rpx
}
.novel-swiper{
height:90vh
}
.card{
height:100%;
width:100%
}
.return-button{
position:absolute;
right:30px;
top:20px;
font-size:25rpx;
font-style:italic;
border:2pxsolidyellow;
border-radius:20%
}
js文件代码:
Page({data:{
novel:[
{
name:"《平凡的世界》",
comment:"中国当代城乡社会生活的长篇小说",
imagePath:"/pages/img/小说1.jpg"
},
{
name:"《骆驼祥子》",
comment:"优秀的现实主义小说",
imagePath:"/pages/img/小说2.jpg"
},
{
name:"《家》",
comment:"入选20世纪中文小说100强(第8位)",
imagePath:"/pages/img/小说3.jpg"
},
{
name:"《悲惨世界》",
comment:"雨果现实主义小说中最成功的一部代表作",
imagePath:"/pages/img/小说4.jpg"
},
],
count:0,
},
onLoad:function(options){
this.setData({
currentindex:this.data.novel.length-1
})
},
f1:function(event){
this.setData({
//新的数据
"novel[3].name":"《巴黎圣母院》",
"novel[3].comment":"是浪漫主义作品中一座里程碑",
"novel[3].imagePath":"/pages/img/小说5.jpg"
})
}
})