多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] ### 1. wxml (模板) ~~~ <view class="like" bind:tap="onLike"> <image src="{{like?yesSrc:noSrc}}" class="icon"></image> <text>{{count}}</text> </view> ~~~ ### 2. js (模板) ~~~ // components/classic/like/index.js Component({ /** * 组件的属性列表 */ properties: { like:{ type:Boolean, value:false }, count:Number }, /** * 组件的初始数据 */ data: { yesSrc:'images/like.png', noSrc:'images/unlike.png' }, /** * 组件的方法列表 */ methods: { onLike(){ var like = this.properties.like; var count = this.properties.count; if(like){ this.setData({ like:false, count:count-1 }) }else{ this.setData({ like:true, count:count+1 }) } var behavior = this.data.like this.triggerEvent('like',{ behavior }) } } }) ~~~ ### 3. wxml(使用) ~~~ <v-like like="{{like}}" count="{{count}}" bind:like="onLike"></v-like> ~~~ ### 4. js (使用) ~~~ onLoad: function (options) { classicModel.getLatest(res => { this.setData({ classic: res, like:res.like_status, count:res.fav_nums }) }) }, onLike(e) { var behavior = e.detail.behavior; var id = this.data.classic.id; var type = this.data.classic.type; likeModel.getLike(behavior, id, type); }, onPrev() { this._updateData("previous"); }, onNext() { this._updateData("next"); }, _updateData(nextOrprevious) { classicModel.getClassic(this.data.classic.index, nextOrprevious, res => { /* 更新点赞的状态 */ likeModel.getLikeStatus(res.type,res.id,res=>{ this.setData({ like:res.like_status, count:res.fav_nums }) }) this.setData({ classic: res, // 更新是否为第一期的状态 isFirst: classicModel.isFirst(res.index), isLatest: classicModel.isLatest(res.index) }) }) } ~~~ ### 5. modeler (再次封装的模块) ~~~ import {HTTP} from "../utils/http"; class LikeModel extends HTTP{ getLike(behavior,id,type){ const url = behavior?"/like":"/like/cancel" this.request({ url, method:"POST", data:{ art_id:id, type } }) } /* 获取期刊点赞的信息 */ getLikeStatus(type,id,callback){ this.request({ url:`/classic/${type}/${id}/favor`, success:res=>{ callback(res); } }) } } export {LikeModel} ~~~