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ù)組每個item定義了一個type,當(dāng)type為support則為 支持;當(dāng)type為unsupport則為不支持;當(dāng)type為空時則為無操作。
點(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ù)減一同時踩的count數(shù)加一;
3.如果是之前踩過,現(xiàn)在點(diǎn)贊,那么則改變它的type為support,并讓頂?shù)腸ount數(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)文章
關(guān)于Element-ui中table默認(rèn)選中toggleRowSelection問題
這篇文章主要介紹了關(guān)于Element-ui中table默認(rèn)選中toggleRowSelection問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
vue2.0中vue-cli實(shí)現(xiàn)全選、單選計算總價格的實(shí)例代碼
本篇文章主要介紹了vue2.0中vue-cli實(shí)現(xiàn)全選、單選計算總價格的實(shí)例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
vue3中使用props和emits并指定其類型與默認(rèn)值
props是Vue3中的一個重要概念,它允許我們將數(shù)據(jù)從父組件傳遞到子組件,下面這篇文章主要給大家介紹了關(guān)于vue3中使用props和emits并指定其類型與默認(rèn)值的相關(guān)資料,需要的朋友可以參考下2023-04-04
Nuxt引用cookie-universal-nuxt在服務(wù)端請求cookie方式
這篇文章主要介紹了Nuxt引用cookie-universal-nuxt在服務(wù)端請求cookie方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
使用Vue+Django+Ant Design做一個留言評論模塊的示例代碼
這篇文章主要介紹了使用Vue+Django+Ant Design做一個留言評論模塊,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
el-element中el-table表格嵌套el-select實(shí)現(xiàn)動態(tài)選擇對應(yīng)值功能
這篇文章主要給大家介紹了關(guān)于el-element中el-table表格嵌套el-select實(shí)現(xiàn)動態(tài)選擇對應(yīng)值功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-01-01
Antd-vue Table組件添加Click事件,實(shí)現(xiàn)點(diǎn)擊某行數(shù)據(jù)教程
這篇文章主要介紹了Antd-vue Table組件添加Click事件,實(shí)現(xiàn)點(diǎn)擊某行數(shù)據(jù)教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11

