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