企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ``` ## //获取组件中的传值 onLike(e) { var behavior = e.detail.behavior; var id = this.data.classic.id; var type = this.data.classic.type; likeModel.getLike(behavior, id, type); } ``` ## 获取更新数据 ``` _updateData(nextOrprevious) { /* 调用models里的获取 缓存或发送http */ 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 }) }) }) } ``` ## models/like.js ``` //HTTP请求(在5.HTTP的封装里) 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} ``` ## like组件 ``` 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 }) } } ``` ## wxml ``` <view class="like"> <image src="{{like?yesSrc:noSrc}}" alt="" bind:tap="onLike" ></image> <text class="count">{{count}}</text> </view> ```