轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 微信小程序 查看主题

小程序request接口的封装,实现给循环列表添加点击样式

发布者: 天生战斗狂V | 发布时间: 2018-5-26 22:31| 查看数: 8580| 评论数: 1|帖子模式

小程序request接口的封装(本质上是对request回调函数再次回调)

  1. module.exports.getData = function (url) {
  2. var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  3. var method = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'POST';
  4. var header = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' };
  5. return new Promise(function (resolve, reject) {
  6. wx.request({
  7. url: url,
  8. data: data,
  9. method: method,
  10. header: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
  11. success: function (res) {
  12. resolve(res)
  13. },
  14. fail: function (res) {
  15. reject(res)
  16. }
  17. })
  18. })
  19. }

调用方法

  1. var common = require("../../common/common.js")
  2. common.getData(postUrl)
  3. .then(function(data){
  4. this.setData({
  5. //....
  6. });
  7. console.log(data);
  8. });

二:实现给循环列表添加点击样式

微信小程序有个属性hover-class=’active’,是指当点击列表元素时当按下鼠标左键会显示active样式,但是鼠标离开样式就会复原.可以参考以下解决方案,直接上代码: 
wxml:

  1. <view class="tags">
  2. <view class="tag-title">标签</view>
  3. <view class="tag-box">
  4. <view wx:for="{{tags}}" wx:key="id" wx:for-index="i">
  5. <view class="tags-item {{currentItem==item.id?'active-tag':''}}" data-id="{{item.id}}" bindtap="tagChoose">{{item.name}}</view>
  6. </view>
  7. </view>
  8. </view>

js文件:

  1. tagChoose:function(options){
  2. var that = this
  3. var id = options.currentTarget.dataset.id;
  4. console.log(id)
  5. //设置当前样式
  6. that.setData({
  7. 'currentItem':id
  8. })
  9. }

核心点:class=”tags-item {{dateCurrent==item.id?’active-tag’:”}}”模板文件中使用三元运算符,通过dateCurrent指定当前item的id

最新评论

张先生 发表于 2022-6-19 17:57
视酷源代码平台

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表