点赞组件



<template>
<view class="zan">
<icon
:class="[up ? 'active' : ' ','iconfont','my-icon-dianzan',]"
@tap.stop="giveLike"
/>
<text class="count">{{count}}</text>
</view>
</template>
<script>
export default{
props:{
praiseCount:{
type:Number
},
onLikeUrl:{
type:String
},
isLikeUrl:{
type:String
}
},
data(){
return {
up:false,
count:this.praiseCount
}
},
methods:{
async getUp(isLikeUrl){
const [errUp,resUp] = await this.request({
url:isLikeUrl,
method:'get'
});
console.log('resUp',resUp);
if(resUp.data.code === 0){
this.up = resUp.data.data;
}else{
uni.showToast({
title:'获取信息失败',
icon:'none'
})
}
},
async giveLike(){
const [err,res] = await this.request({
url:this.onLikeUrl,
method:'get'
});
if(res.data.code === 0){
if(this.up){
this.up = !this.up;
this.count -= 1;
}else{
this.up = !this.up;
this.count += 1;
}
this.$emit('changeLike',this.count);
}else{
uni.showToast({
title:'点赞失败',
icon:'none'
})
}
}
},
watch:{
praiseCount(newCount,oldCount){
this.count = newCount;
this.getUp(this.isLikeUrl);
}
},
created(){
console.log('isLikeUrl',this.isLikeUrl);
this.getUp(this.isLikeUrl);
}
}
</script>
<style>
.active{
color:rgb(255,102,0);
}
</style>
复制代码
父组件
<Praise class="zan" :praiseCount="praiseCount" :onLikeUrl="onLikeUrl" :isLikeUrl="isLikeUrl" @changeLike="changeLike"></Praise>
复制代码