VUE利用vuex模擬實(shí)現(xiàn)新聞點(diǎn)贊功能實(shí)例
回顧新聞詳細(xì)頁
很早我們的新聞詳情頁是在news-detail.vue 組件里,獲取服務(wù)器數(shù)據(jù),然后把數(shù)據(jù)保持到組件的data 里,既然我們已經(jīng)用到了vuex,學(xué)習(xí)了它的state,我們就應(yīng)該想到把返回的數(shù)據(jù)交給state 來存儲。
1.首先在Vuex.Store 實(shí)例化的時(shí)候:
state:{ user_name:"", newslist:[], newsdetail:{} },
增加一個(gè)newsdetail 對象,newslist 數(shù)組是我們前面用來保存新聞列表數(shù)據(jù)的。
2.下面就要看在news-detail.vue 組件里,怎么請求數(shù)據(jù),然后交給newsdatail :
<script> export default{ // 創(chuàng)建的時(shí)候[生命周期里] created(){ this.$http.get("http://localhost/newsdetail.php?id="+this.$route.params.newsid).then(function(res){ this.$store.state.newsdetail = res.body; },function(res){ // 處理請求失敗 }); }, } </script>
通過this.$store.state.newsdetail = res.body;
就把服務(wù)器返回的新聞詳細(xì)數(shù)據(jù)保存起來了。
3.那么模板上怎么展示?
<div class="page-header"> <h2>{{this.$store.state.newsdetail.title}}<small>{{this.$store.state.newsdetail.pubtime}}</small></h2> <p>點(diǎn)贊數(shù):{{this.$store.state.newsdetail.agree}} <button class="btn btn-success">點(diǎn)贊</button></p> <p>{{this.$store.state.newsdetail.desc}}</p> </div>
這里我們要來實(shí)現(xiàn)一個(gè)點(diǎn)贊功能
點(diǎn)擊“點(diǎn)贊”按鈕,就更改點(diǎn)擊數(shù)。
其實(shí)就是更改newsdetail 里的agree 屬性。
本文參考文檔:https://vuefe.cn/vuex/actions.html
import Vuex from 'vuex'; Vue.use(Vuex); const vuex_store = new Vuex.Store({ state:{ user_name:"", newslist:[], newsdetail:{} }, mutations:{ showUserName(state){ alert(state.user_name); }, setAgree(state,agreeNum){ state.newsdetail.agree = agreeNum; } }, actions:{ agree(context,newsid){ // 進(jìn)行請求,獲取點(diǎn)贊后的agree字段屬性值 Vue.http.post("http://localhost/agree.php",{newsid:newsid},{emulateJSON:true}).then(function (res) { // 處理業(yè)務(wù) // 調(diào)用上面setAgree方法更新點(diǎn)贊數(shù) context.commit("setAgree",res.body.agree); },function(){}) } }, getters:{ getNews(state){ return state.newslist.filter(function (news) { return !news.isdeleted; }) } } })
在actions
里定義了一個(gè)方法agree
發(fā)送網(wǎng)絡(luò)請求,獲取最新的點(diǎn)贊數(shù)。
同時(shí)mutations
里定義了一個(gè)setAgree
方法,用來同步頁面上的點(diǎn)贊數(shù)。
agree(context,newsid){ // 進(jìn)行請求,獲取點(diǎn)贊后的agree字段屬性值 Vue.http.post("http://localhost/agree.php",{newsid:newsid},{emulateJSON:true}).then(function (res) { // 處理業(yè)務(wù) // 調(diào)用上面setAgree方法更新點(diǎn)贊數(shù) context.commit("setAgree",res.body.agree); },function(){}) }
重點(diǎn)說明:這里發(fā)送http請求,和組件里不一樣,需要注意。
那么,組件里怎么調(diào)用這里的agree 方法呢?
<button class="btn btn-success" @click="submitAgree">點(diǎn)贊</button>
methods:{ submitAgree(){ // 組件了調(diào)用actions里定義的agree方法 this.$store.dispatch("agree",this.$store.state.newsdetail.id) } }
news-detail.vue 組件全部代碼:
<template> <div class="news-detail"> <div class="row"> <div class="page-header"> <h2>{{this.$store.state.newsdetail.title}}<small>{{this.$store.state.newsdetail.pubtime}}</small></h2> <p>點(diǎn)贊數(shù):{{this.$store.state.newsdetail.agree}} <button class="btn btn-success" @click="submitAgree">點(diǎn)贊</button></p> <p>{{this.$store.state.newsdetail.desc}}</p> </div> </div> </div> </template> <script> export default{ // 創(chuàng)建的時(shí)候[生命周期里] created(){ this.$http.get("http://localhost/newsdetail.php?id="+this.$route.params.newsid).then(function(res){ this.$store.state.newsdetail = res.body; },function(res){ // 處理請求失敗 }); }, methods:{ submitAgree(){ // 組件了調(diào)用actions里定義的agree方法 this.$store.dispatch("agree",this.$store.state.newsdetail.id) } } } </script>
后端程序增加點(diǎn)贊數(shù),這里就不贅述了。只需返回一個(gè)json對象:
{"status":"success","agree":100}
以上就是本文的全部內(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開發(fā)實(shí)現(xiàn)評論列表
- vue實(shí)現(xiàn)評論列表
- Vue實(shí)現(xiàn)簡單的發(fā)表評論功能
- vue實(shí)現(xiàn)文章點(diǎn)贊和差評功能
相關(guān)文章
Vite創(chuàng)建項(xiàng)目的實(shí)現(xiàn)步驟
隨著 Vite2 的發(fā)布并日趨穩(wěn)定,現(xiàn)在越來越多的項(xiàng)目開始嘗試使用它。本文我們就介紹了Vite創(chuàng)建項(xiàng)目的實(shí)現(xiàn)步驟,感興趣的可以了解一下2021-07-07在vue中使用Echarts利用watch做動態(tài)數(shù)據(jù)渲染操作
這篇文章主要介紹了在vue中使用Echarts利用watch做動態(tài)數(shù)據(jù)渲染操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07ElementUI Tag組件實(shí)現(xiàn)多標(biāo)簽生成的方法示例
這篇文章主要介紹了ElementUI Tag組件實(shí)現(xiàn)多標(biāo)簽生成的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07vue安裝node-sass和sass-loader報(bào)錯(cuò)問題的解決辦法
這篇文章主要給大家介紹了關(guān)于vue安裝node-sass和sass-loader報(bào)錯(cuò)問題的解決辦法,文中通過圖文以及示例代碼將解決的方法介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-01-01vue?element?ui?Select選擇器如何設(shè)置默認(rèn)狀態(tài)
這篇文章主要介紹了vue?element?ui?Select選擇器如何設(shè)置默認(rèn)狀態(tài)問題,具有很好的參考價(jià)值,希望對大家有所幫助,2023-10-10Vue項(xiàng)目通過network的ip地址訪問注意事項(xiàng)及說明
這篇文章主要介紹了Vue項(xiàng)目通過network的ip地址訪問注意事項(xiàng)及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09詳解vue computed的緩存實(shí)現(xiàn)原理
這篇文章主要介紹了vue computed的緩存實(shí)現(xiàn)原理,幫助大家更好的理解和學(xué)習(xí)使用vue,感興趣的朋友可以了解下2021-04-04