国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

VUE利用vuex模擬實現新聞點贊功能實例

2019-11-19 16:13:39
字體:
來源:轉載
供稿:網友

回顧新聞詳細頁

很早我們的新聞詳情頁是在news-detail.vue 組件里,獲取服務器數據,然后把數據保持到組件的data 里,既然我們已經用到了vuex,學習了它的state,我們就應該想到把返回的數據交給state 來存儲。

1.首先在Vuex.Store 實例化的時候:

  state:{    user_name:"",    newslist:[],    newsdetail:{}  },

增加一個newsdetail 對象,newslist 數組是我們前面用來保存新聞列表數據的。

2.下面就要看在news-detail.vue 組件里,怎么請求數據,然后交給newsdatail :

<script>  export default{    // 創建的時候[生命周期里]    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; 就把服務器返回的新聞詳細數據保存起來了。

3.那么模板上怎么展示?

<div class="page-header">  <h2>{{this.$store.state.newsdetail.title}}<small>{{this.$store.state.newsdetail.pubtime}}</small></h2>  <p>點贊數:{{this.$store.state.newsdetail.agree}} <button class="btn btn-success">點贊</button></p>  <p>{{this.$store.state.newsdetail.desc}}</p></div>

這里寫圖片描述

這里我們要來實現一個點贊功能

點擊“點贊”按鈕,就更改點擊數。

其實就是更改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){      // 進行請求,獲取點贊后的agree字段屬性值      Vue.http.post("http://localhost/agree.php",{newsid:newsid},{emulateJSON:true}).then(function (res) {        // 處理業務        // 調用上面setAgree方法更新點贊數        context.commit("setAgree",res.body.agree);      },function(){})    }  },  getters:{    getNews(state){      return state.newslist.filter(function (news) {        return !news.isdeleted;      })    }  }})

actions 里定義了一個方法agree 發送網絡請求,獲取最新的點贊數。

同時mutations 里定義了一個setAgree 方法,用來同步頁面上的點贊數。

    agree(context,newsid){      // 進行請求,獲取點贊后的agree字段屬性值      Vue.http.post("http://localhost/agree.php",{newsid:newsid},{emulateJSON:true}).then(function (res) {        // 處理業務        // 調用上面setAgree方法更新點贊數        context.commit("setAgree",res.body.agree);      },function(){})    }

重點說明:這里發送http請求,和組件里不一樣,需要注意。

那么,組件里怎么調用這里的agree 方法呢?

<button class="btn btn-success" @click="submitAgree">點贊</button>
    methods:{      submitAgree(){        // 組件了調用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>點贊數:{{this.$store.state.newsdetail.agree}} <button class="btn btn-success" @click="submitAgree">點贊</button></p>        <p>{{this.$store.state.newsdetail.desc}}</p>      </div>    </div>  </div></template>  <script>  export default{    // 創建的時候[生命周期里]    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(){        // 組件了調用actions里定義的agree方法        this.$store.dispatch("agree",this.$store.state.newsdetail.id)      }    }  }</script>

這里寫圖片描述 

后端程序增加點贊數,這里就不贅述了。只需返回一個json對象:

{"status":"success","agree":100}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青川县| 潮州市| 宣威市| 伊金霍洛旗| 县级市| 房产| 来凤县| 佳木斯市| 高阳县| 鄱阳县| 潮安县| 鄄城县| 凤翔县| 永年县| 安塞县| 神木县| 门源| 南宫市| 思南县| 远安县| 德惠市| 札达县| 邢台县| 抚州市| 广平县| 大连市| 易门县| 黄龙县| 巴林右旗| 沂水县| 古交市| 黄浦区| 平泉县| 五家渠市| 阿鲁科尔沁旗| 阳谷县| 阳谷县| 阳谷县| 长寿区| 库尔勒市| 边坝县|