VUE利用vuex模擬實(shí)現(xiàn)新聞點(diǎn)贊功能實(shí)例
回顧新聞詳細(xì)頁(yè)
很早我們的新聞詳情頁(yè)是在news-detail.vue 組件里,獲取服務(wù)器數(shù)據(jù),然后把數(shù)據(jù)保持到組件的data 里,既然我們已經(jīng)用到了vuex,學(xué)習(xí)了它的state,我們就應(yīng)該想到把返回的數(shù)據(jù)交給state 來(lái)存儲(chǔ)。
1.首先在Vuex.Store 實(shí)例化的時(shí)候:
state:{
user_name:"",
newslist:[],
newsdetail:{}
},
增加一個(gè)newsdetail 對(duì)象,newslist 數(shù)組是我們前面用來(lái)保存新聞列表數(shù)據(jù)的。
2.下面就要看在news-detail.vue 組件里,怎么請(qǐng)求數(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){
// 處理請(qǐng)求失敗
});
},
}
</script>
通過(guò)this.$store.state.newsdetail = res.body; 就把服務(wù)器返回的新聞詳細(xì)數(shù)據(jù)保存起來(lái)了。
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>

這里我們要來(lái)實(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)行請(qǐng)求,獲取點(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ò)請(qǐng)求,獲取最新的點(diǎn)贊數(shù)。
同時(shí)mutations 里定義了一個(gè)setAgree 方法,用來(lái)同步頁(yè)面上的點(diǎn)贊數(shù)。
agree(context,newsid){
// 進(jìn)行請(qǐng)求,獲取點(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)說(shuō)明:這里發(fā)送http請(qǐng)求,和組件里不一樣,需要注意。
那么,組件里怎么調(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){
// 處理請(qǐng)求失敗
});
},
methods:{
submitAgree(){
// 組件了調(diào)用actions里定義的agree方法
this.$store.dispatch("agree",this.$store.state.newsdetail.id)
}
}
}
</script>
后端程序增加點(diǎn)贊數(shù),這里就不贅述了。只需返回一個(gè)json對(duì)象:
{"status":"success","agree":100}
以上就是本文的全部?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開(kāi)發(fā)實(shí)現(xiàn)評(píng)論列表
- vue實(shí)現(xiàn)評(píng)論列表
- Vue實(shí)現(xiàn)簡(jiǎn)單的發(fā)表評(píng)論功能
- vue實(shí)現(xiàn)文章點(diǎn)贊和差評(píng)功能
相關(guān)文章
Vite創(chuàng)建項(xiàng)目的實(shí)現(xiàn)步驟
隨著 Vite2 的發(fā)布并日趨穩(wěn)定,現(xiàn)在越來(lái)越多的項(xiàng)目開(kāi)始嘗試使用它。本文我們就介紹了Vite創(chuàng)建項(xiàng)目的實(shí)現(xiàn)步驟,感興趣的可以了解一下2021-07-07
在vue中使用Echarts利用watch做動(dòng)態(tài)數(shù)據(jù)渲染操作
這篇文章主要介紹了在vue中使用Echarts利用watch做動(dòng)態(tài)數(shù)據(jù)渲染操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
ElementUI Tag組件實(shí)現(xiàn)多標(biāo)簽生成的方法示例
這篇文章主要介紹了ElementUI Tag組件實(shí)現(xiàn)多標(biāo)簽生成的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
vue安裝node-sass和sass-loader報(bào)錯(cuò)問(wèn)題的解決辦法
這篇文章主要給大家介紹了關(guān)于vue安裝node-sass和sass-loader報(bào)錯(cuò)問(wèn)題的解決辦法,文中通過(guò)圖文以及示例代碼將解決的方法介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-01-01
vue?element?ui?Select選擇器如何設(shè)置默認(rèn)狀態(tài)
這篇文章主要介紹了vue?element?ui?Select選擇器如何設(shè)置默認(rèn)狀態(tài)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,2023-10-10
Vue項(xiàng)目通過(guò)network的ip地址訪問(wèn)注意事項(xiàng)及說(shuō)明
這篇文章主要介紹了Vue項(xiàng)目通過(guò)network的ip地址訪問(wèn)注意事項(xiàng)及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Vue?+?Axios?請(qǐng)求接口方法與傳參方式詳解
使用Vue的腳手架搭建的前端項(xiàng)目,通常都使用Axios封裝的接口請(qǐng)求,項(xiàng)目中引入的方式不做多介紹,本文主要介紹接口調(diào)用與不同形式的傳參方法。對(duì)Vue?+?Axios?請(qǐng)求接口方法與傳參問(wèn)題感興趣的朋友一起看看吧2021-12-12
詳解vue computed的緩存實(shí)現(xiàn)原理
這篇文章主要介紹了vue computed的緩存實(shí)現(xiàn)原理,幫助大家更好的理解和學(xué)習(xí)使用vue,感興趣的朋友可以了解下2021-04-04

