vue實(shí)現(xiàn)文章點(diǎn)贊和差評(píng)功能
本文實(shí)例為大家分享了vue實(shí)現(xiàn)文章點(diǎn)贊和差評(píng)功能的具體代碼,供大家參考,具體內(nèi)容如下
純前端實(shí)現(xiàn)文章點(diǎn)贊與差評(píng)(支持與不支持)。
需求:狀態(tài)1:用戶(hù)沒(méi)有操作過(guò),即既沒(méi)點(diǎn)贊和差評(píng);狀態(tài)二:用戶(hù)點(diǎn)贊過(guò);狀態(tài)三:用戶(hù)差評(píng)過(guò)。點(diǎn)贊或差評(píng)過(guò)后無(wú)法取消,可切換。如下圖:

dom結(jié)構(gòu):
<!-- 頂 -->
? ? ? <view class="flex-1 flex ai-center jc-center animate__animated animate__fast" hover-class="animate__jello text-main"
? ? ? ? @click="DoSupport('support')" :class="item.support.type === 'support'? 'support-active':''">
? ? ? ? <text class="iconfont icon-dianzan2 mr-2"></text>
? ? ? ? <text>{{item.support.support_count}}</text>
? ? ? </view>
? ? ? <!-- 踩 -->
? ? ? <view class="flex-1 flex ai-center jc-center animate__animated animate__fast" hover-class="animate__jello text-main"
? ? ? ? @click="DoSupport('unsupport')" :class="item.support.type === 'unsupport'? 'support-active':''">
? ? ? ? <text class="iconfont icon-cai mr-2"></text>
? ? ? ? <text>{{item.support.unsupport_count}}</text>
</view>list數(shù)據(jù):
const demo = [{
? ? username: "昵稱(chēng)",
? ? userpic: "/static/tabber/indexSelected.png",
? ? newstime: "2021-1-1 下午1:00",
? ? isFollow: false,
? ? title: "我是標(biāo)題",
? ? titlepic: "/static/2956568.jpg",
? ? support: {
? ? ? type: "support", //支持
? ? ? support_count: 1,
? ? ? unsupport_count: 2
? ? },
? ? comment_count: 2,
? ? share_num: 2
? }, {
? ? username: "昵稱(chēng)",
? ? userpic: "/static/tabber/indexSelected.png",
? ? newstime: "2021-1-1 下午1:00",
? ? isFollow: false,
? ? title: "我是標(biāo)題",
? ? titlepic: "/static/2956568.jpg",
? ? support: {
? ? ? type: "unsupport", //不支持
? ? ? support_count: 1,
? ? ? unsupport_count: 2
? ? },
? ? comment_count: 2,
? ? share_num: 2
? }, {
? ? username: "昵稱(chēng)",
? ? userpic: "/static/tabber/indexSelected.png",
? ? newstime: "2021-1-1 下午1:00",
? ? isFollow: false,
? ? title: "我是標(biāo)題",
? ? titlepic: "/static/2956568.jpg",
? ? support: {
? ? ? type: "", //無(wú)操作
? ? ? support_count: 1,
? ? ? unsupport_count: 2
? ? },
? ? comment_count: 2,
? ? share_num: 2
? }]list數(shù)組每個(gè)item定義了一個(gè)type,當(dāng)type為support則為 支持;當(dāng)type為unsupport則為不支持;當(dāng)type為空時(shí)則為無(wú)操作。
點(diǎn)擊方法:點(diǎn)擊之后子組件通知父組件并傳遞點(diǎn)擊的是哪篇文章(index),點(diǎn)擊的是贊還是踩(支持還是不支持)
// 頂踩操作
DoSupport(type) {
? ? ? ? // 通知父組件
? ? ? ? this.$emit('doSupport', {
? ? ? ? ? type: type,
? ? ? ? ? index: this.index
? ? ? ? })
}父組件中接收:
邏輯是:
拿到當(dāng)前對(duì)象:let item = this.list[e.index]
1.如果是之前沒(méi)有操作過(guò),則改變它的type,并讓它的相對(duì)應(yīng)的count加1;
2.如果是之前頂過(guò),現(xiàn)在點(diǎn)踩,那么則改變它的type為unsupport,并讓頂?shù)腸ount數(shù)減一同時(shí)踩的count數(shù)加一;
3.如果是之前踩過(guò),現(xiàn)在點(diǎn)贊,那么則改變它的type為support,并讓頂?shù)腸ount數(shù)加一同時(shí)踩的count數(shù)減一;
// 頂踩操作
doSupport(e) {
? ? ? ? // 拿到當(dāng)前對(duì)象
? ? ? ? let item = this.list[e.index]
? ? ? ? // 之前沒(méi)有操作過(guò)
? ? ? ? if (item.support.type === '') {
? ? ? ? ? item.support.type = e.type
? ? ? ? ? item.support[e.type + '_count']++
? ? ? ? ? return
? ? ? ? }
? ? ? ? // 之前頂過(guò)
? ? ? ? if (item.support.type === 'support' && e.type === 'unsupport') {
? ? ? ? ? item.support.type = e.type
? ? ? ? ? // 踩+1
? ? ? ? ? item.support.unsupport_count++
? ? ? ? ? // 頂-1
? ? ? ? ? item.support.support_count--
? ? ? ? ? return
? ? ? ? }
? ? ? ? // 之前踩過(guò)
? ? ? ? if (item.support.type === 'unsupport' && e.type === 'support') {
? ? ? ? ? item.support.type = e.type
? ? ? ? ? // 頂+1
? ? ? ? ? item.support.support_count++
? ? ? ? ? // 踩-1
? ? ? ? ? item.support.unsupport_count--
? ? ? ? ? return
? ? ? ? }
? ? ? },如此,文章的點(diǎn)贊與差評(píng)的代碼已完成。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue實(shí)現(xiàn)靜態(tài)頁(yè)面點(diǎn)贊和取消點(diǎn)贊功能
- 解決vue-seamless-scroll滾動(dòng)加點(diǎn)贊銜接處數(shù)據(jù)不同步問(wèn)題
- Vue transition實(shí)現(xiàn)點(diǎn)贊動(dòng)畫(huà)效果的示例
- Vue+Bootstrap收藏(點(diǎn)贊)功能邏輯與具體實(shí)現(xiàn)
- vue實(shí)現(xiàn)直播間點(diǎn)贊飄心效果的示例代碼
- vue組件之間通信實(shí)例總結(jié)(點(diǎn)贊功能)
- VUE利用vuex模擬實(shí)現(xiàn)新聞點(diǎn)贊功能實(shí)例
- vue開(kāi)發(fā)實(shí)現(xiàn)評(píng)論列表
- vue實(shí)現(xiàn)評(píng)論列表
- Vue實(shí)現(xiàn)簡(jiǎn)單的發(fā)表評(píng)論功能
相關(guān)文章
vue+iview 實(shí)現(xiàn)可編輯表格的示例代碼
這篇文章主要介紹了vue+iview 實(shí)現(xiàn)可編輯表格的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
vue-resource調(diào)用promise取數(shù)據(jù)方式詳解
這篇文章主要介紹了vue-resource調(diào)用promise取數(shù)據(jù)方式詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
vue中實(shí)現(xiàn)支持txt,docx,xlsx,mp4格式文件預(yù)覽功能(純前端)
對(duì)于Vue你可以實(shí)現(xiàn)文件的預(yù)覽功能,這篇文章主要給大家介紹了關(guān)于vue中實(shí)現(xiàn)支持txt,docx,xlsx,mp4格式文件預(yù)覽功能的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
vue解決this.$refs.xx在mounted中獲取DOM元素為undefined問(wèn)題
這篇文章主要介紹了vue解決this.$refs.xx在mounted中獲取DOM元素為undefined問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Vue利用draggable實(shí)現(xiàn)多選拖拽效果
這篇文章主要為大家詳細(xì)介紹了如何利用vue中的draggable插件實(shí)現(xiàn)多選拖拽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
vue項(xiàng)目打包部署后默認(rèn)路由不正確的解決方案
這篇文章主要介紹了vue項(xiàng)目打包部署后默認(rèn)路由不正確的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04

