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 來存儲(chǔ)。
1.首先在Vuex.Store 實(shí)例化的時(shí)候:
state:{
user_name:"",
newslist:[],
newsdetail:{}
},
增加一個(gè)newsdetail 對(duì)象,newslist 數(shù)組是我們前面用來保存新聞列表數(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>
通過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)行請(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 方法,用來同步頁面上的點(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)說明:這里發(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}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue實(shí)現(xiàn)靜態(tài)頁面點(diǎn)贊和取消點(diǎn)贊功能
- 解決vue-seamless-scroll滾動(dòng)加點(diǎn)贊銜接處數(shù)據(jù)不同步問題
- Vue transition實(shí)現(xiàn)點(diǎn)贊動(dòng)畫效果的示例
- Vue+Bootstrap收藏(點(diǎn)贊)功能邏輯與具體實(shí)現(xiàn)
- vue實(shí)現(xiàn)直播間點(diǎn)贊飄心效果的示例代碼
- vue組件之間通信實(shí)例總結(jié)(點(diǎn)贊功能)
- vue開發(fā)實(shí)現(xiàn)評(píng)論列表
- vue實(shí)現(xiàn)評(píng)論列表
- Vue實(shí)現(xiàn)簡單的發(fā)表評(píng)論功能
- vue實(shí)現(xiàn)文章點(diǎn)贊和差評(píng)功能
相關(guān)文章
Vue 2.0學(xué)習(xí)筆記之使用$refs訪問Vue中的DOM
這篇文章主要介紹了Vue 2.0學(xué)習(xí)筆記之使用$refs訪問Vue中的DOM,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
VUE配置proxy代理的開發(fā)測(cè)試及生產(chǎn)環(huán)境
這篇文章主要為大家介紹了VUE配置proxy代理的開發(fā)環(huán)境、測(cè)試環(huán)境、生產(chǎn)環(huán)境詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Vue設(shè)置提示和警告彈出框?qū)崙?zhàn)案例
頁面中會(huì)有很多時(shí)候需要彈窗提示,下面這篇文章主要給大家介紹了關(guān)于Vue設(shè)置提示和警告彈出框的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02
Vuex利用state保存新聞數(shù)據(jù)實(shí)例
本篇文章主要介紹了Vuex利用state保存新聞數(shù)據(jù)實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
vue父組件觸發(fā)事件改變子組件的值的方法實(shí)例詳解
這篇文章主要介紹了vue父組件觸發(fā)事件改變子組件的值的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
解決基于 keep-alive 的后臺(tái)多級(jí)路由緩存問題
這篇文章主要介紹了解決基于 keep-alive 的后臺(tái)多級(jí)路由緩存問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12

