欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

VUE利用vuex模擬實(shí)現(xiàn)新聞點(diǎn)贊功能實(shí)例

 更新時(shí)間:2017年06月28日 16:21:31   作者:學(xué)習(xí)筆記666  
本篇文章主要介紹了VUE利用vuex模擬實(shí)現(xiàn)新聞點(diǎn)贊功能實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

回顧新聞詳細(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue 實(shí)現(xiàn)圖片懶加載功能

    vue 實(shí)現(xiàn)圖片懶加載功能

    這篇文章主要介紹了vue 實(shí)現(xiàn)圖片懶加載功能的方法,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下
    2020-12-12
  • Vite創(chuàng)建項(xiàng)目的實(shí)現(xià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ù)渲染操作

    這篇文章主要介紹了在vue中使用Echarts利用watch做動態(tài)數(shù)據(jù)渲染操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • ElementUI Tag組件實(shí)現(xiàn)多標(biāo)簽生成的方法示例

    ElementUI 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-07
  • vue安裝node-sass和sass-loader報(bào)錯(cuò)問題的解決辦法

    vue安裝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-01
  • vue?element?ui?Select選擇器如何設(shè)置默認(rèn)狀態(tài)

    vue?element?ui?Select選擇器如何設(shè)置默認(rèn)狀態(tài)

    這篇文章主要介紹了vue?element?ui?Select選擇器如何設(shè)置默認(rèn)狀態(tài)問題,具有很好的參考價(jià)值,希望對大家有所幫助,
    2023-10-10
  • VUEX如何使用

    VUEX如何使用

    Vuex?可以幫助我們管理共享狀態(tài),并附帶了更多的概念和框架,本文主要介紹了VUEX如何使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Vue項(xiàng)目通過network的ip地址訪問注意事項(xiàng)及說明

    Vue項(xiàng)目通過network的ip地址訪問注意事項(xiàng)及說明

    這篇文章主要介紹了Vue項(xiàng)目通過network的ip地址訪問注意事項(xiàng)及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Vue?+?Axios?請求接口方法與傳參方式詳解

    Vue?+?Axios?請求接口方法與傳參方式詳解

    使用Vue的腳手架搭建的前端項(xiàng)目,通常都使用Axios封裝的接口請求,項(xiàng)目中引入的方式不做多介紹,本文主要介紹接口調(diào)用與不同形式的傳參方法。對Vue?+?Axios?請求接口方法與傳參問題感興趣的朋友一起看看吧
    2021-12-12
  • 詳解vue computed的緩存實(shí)現(xiàn)原理

    詳解vue computed的緩存實(shí)現(xiàn)原理

    這篇文章主要介紹了vue computed的緩存實(shí)現(xiàn)原理,幫助大家更好的理解和學(xué)習(xí)使用vue,感興趣的朋友可以了解下
    2021-04-04

最新評論