最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解讀vue頁面監(jiān)聽store值改變問題

 更新時間:2022年10月24日 09:09:56   作者:榴蓮豆包  
這篇文章主要介紹了解讀vue頁面監(jiān)聽store值改變問題,具有很好的參考價值,希望對大家有所幫助。

vue頁面監(jiān)聽store值改變

首先建立store:

import router, { rootRoute, exceptionRoutes, filterAsyncRoutes } from '@/routes'
import asyncRoutes from '@/routes/asyncRoutes'
import config from '@/utils/config'
import { length } from '@/utils'
import request from '@/api'
 
export default {
  namespaced: true,
  state: {
    messageList:[],//消息列表
  },
  mutations: {
    //聊天消息儲存
    SET_MESSAGELIST:(state, info)=>{
      let data = Object.assign([], state.messageList ,info)
      state.messageList = data
    },
  },
  actions: {
    
    },
    // 同步緩存頁面
    AsyncIncludes ({ commit, state }, data) {
      commit('SET_INCLUDES', data)
    },
    // 新增緩存頁面
    AddIncludes ({ commit, state }, path) {
      let includes = state.includes
      if (includes.indexOf(path) < 0) {
        includes.push(path)
      }
      commit('SET_INCLUDES', includes)
    },
    // 刪除緩存頁面
    DelIncludes ({ commit, state }, path) {
      let includes = state.includes.filter(i => i !== path)
      commit('SET_INCLUDES', includes)
    },
    // 退出
    Logout ({ commit }) {
      commit('SET_OUT')
    }
  },
  getters: {
    includes: state => state.includes,
    get_CustomerList: state => state.customerList,
    get_ExpertList: state => state.expertList,
  }
}

重點(diǎn)是:

SET_MESSAGELIST:(state, info)=>{
? ? ? let data = Object.assign([], state.messageList ,info)
? ? ? state.messageList = data
? ? }

然后是存值:

hat.$store.commit('permission/SET_MESSAGELIST', that.conversationList)

一定帶上模塊名稱(permission)。

然后是使用computed計算屬性取值:

?computed: {
?
? ? ? ? cuserList() {
?
? ? ? ? ? ? return this.$store.state.permission.messageList;
?
? ? ? ? },
?
? ? },

使用深度監(jiān)聽新值變化:

watch:{ ? ? //監(jiān)聽value的變化,進(jìn)行相應(yīng)的操做便可
? ? fuid: function(a,b){ ? ? //a是value的新值,b是舊值
? ? ? this.uid = this.fuid;
? ? ? this.chatList =[]
? ? ? this.getChatList();
? ? },
? ? cuserList: {
? ? ? ? handler(nval, oval) {
? ? ? ? ? debugger
? ? ? ? ? if(nval.length>0){
? ? ? ? ? ? ? this.userList.forEach(item=>{
? ? ? ? ? ? ? ? nval.forEach(item2=>{
? ? ? ? ? ? ? ? ? if(item.uid==item2.send_uid && this.uid ==item2.receive_uid){
? ? ? ? ? ? ? ? ? ?item.unreadCount = item.unreadCount+1;
? ? ? ? ? ? ? ? ? ?item.msg_type = item2.msg_type;
? ? ? ? ? ? ? ? ? ?item.msg_content = item2.msg_content;
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? if(item.uid==item2.send_uid && this.uid ==item2.receive_uid){
? ? ? ? ? ? ? ? ? ?item.unreadCount = item.unreadCount+1;
? ? ? ? ? ? ? ? ? ?item.msg_type = item2.msg_type;
? ? ? ? ? ? ? ? ? ?item.msg_content = item2.msg_content;
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? })
? ? ? ? ? ? ? console.log(this.userList)
? ? ? ? ? }
? ? ? ? },
? ? ? ? deep: true, // 深度監(jiān)聽
? ? ? ? immediate: true,//立即執(zhí)行
? ? },
? },

完畢,這樣能解決絕大部分問題。 

vue監(jiān)聽store.state對象變化不起作用

store.state中的對象屬性發(fā)生改變,引用state的組件中卻監(jiān)聽不到變化,深度監(jiān)聽也不起作用,如下代碼:

// state.js
noticeParams: [
? ? { CODE: null, NoticeType: null},
? ? { CODE: null, NoticeType: null},
? ? { CODE: null, NoticeType: null}
? ]
// 所引用組件
export default {
? methods: {
? ? profileJump(path, tab) {
? ? ? this.$router.push({ path: path, query: { tab: tab } });
? ? }
? },
? computed: {
? ? ...mapState(['noticeParams'])
? },
? watch: {
? ? noticeParams(val){
? ? ? console.log('HomeHeader=====>', val);
? ? }
? }
};
// 重新賦值
computed: {
? ? ...mapState(['noticeParams'])
? },
methods:{
?? ?fn(){
?? ??? ?// 省略了一些操作
?? ??? ?this.$store.commit('setNoticeParams', this.noticeParams)
?? ?}
}
?// mutations里的方法
?setNoticeParams(state, data) {
??? ?// 問題就出現(xiàn)在此處
? ? state.noticeParams = data
? }

由于重新賦值的代碼中里引用了初始 state.noticeParams,而mutations的方法中將改變后的state.noticeParams又重新賦值給state.noticeParams,此時state.noticeParams里的屬性值發(fā)生了改變但引用并未發(fā)生變化,所以監(jiān)聽沒起作用,解決方法就是創(chuàng)建新的數(shù)組

setNoticeParams(state, data) {
? ? let arr = Object.assign([], state.noticeParams, data);
? ? state.noticeParams = arr
? ? // 創(chuàng)建一個空數(shù)組,將初始state.noticeParams與改變后的state.noticeParams==》data合并,最后賦值給state.noticeParams
? }

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue二次封裝axios流程詳解

    Vue二次封裝axios流程詳解

    在vue項(xiàng)目中,和后臺交互獲取數(shù)據(jù)這塊,我們通常使用的是axios庫,它是基于promise的http庫,下面這篇文章主要給大家介紹了關(guān)于Vue3引入axios封裝接口的兩種方法,需要的朋友可以參考下
    2022-10-10
  • Element Breadcrumb 面包屑的使用方法

    Element Breadcrumb 面包屑的使用方法

    這篇文章主要介紹了Element Breadcrumb 面包屑的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • vuecli+AXdownload下載組件封裝?+css3下載懸浮球動畫效果

    vuecli+AXdownload下載組件封裝?+css3下載懸浮球動畫效果

    當(dāng)觸發(fā)下載功能的時候,會觸發(fā)一個下載動畫,下載懸浮球會自動彈出,并且閃爍提示有新的下載任務(wù)直到下載任務(wù)完場提示,接下來通過本文介紹vuecli+AXdownload下載組件封裝?+css3下載懸浮球動畫效果,需要的朋友可以參考下
    2024-05-05
  • vue2實(shí)現(xiàn)簡易時鐘效果

    vue2實(shí)現(xiàn)簡易時鐘效果

    這篇文章主要為大家詳細(xì)介紹了vue2實(shí)現(xiàn)簡易時鐘效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Vue中的$set的使用實(shí)例代碼

    Vue中的$set的使用實(shí)例代碼

    這篇文章主要介紹了Vue中的$set的使用,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-10-10
  • vue App.vue中的公共組件改變值觸發(fā)其他組件或.vue頁面監(jiān)聽

    vue App.vue中的公共組件改變值觸發(fā)其他組件或.vue頁面監(jiān)聽

    這篇文章主要介紹了vue App.vue里的公共組件改變值觸發(fā)其他組件或.vue頁面監(jiān)聽,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • vue axios請求成功卻進(jìn)入catch的原因分析

    vue axios請求成功卻進(jìn)入catch的原因分析

    這篇文章主要介紹了vue axios請求成功卻進(jìn)入catch的原因分析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 淺談axios中取消請求及阻止重復(fù)請求的方法

    淺談axios中取消請求及阻止重復(fù)請求的方法

    在實(shí)際項(xiàng)目中,我們可能需要對請求進(jìn)行“防抖”處理。本文主要實(shí)現(xiàn)axios中取消請求及阻止重復(fù)請求,具有一定的參考價值,感興趣的可以了解一下
    2021-08-08
  • vue.js整合mint-ui里的輪播圖實(shí)例代碼

    vue.js整合mint-ui里的輪播圖實(shí)例代碼

    這篇文章主要介紹了vue.js整合mint-ui里的輪播圖的方法,首先我們需要初始化vue項(xiàng)目,然后安裝mint-ui。具體內(nèi)容詳情大家通過學(xué)習(xí)
    2017-12-12
  • Vue EventBus自定義組件事件傳遞

    Vue EventBus自定義組件事件傳遞

    這篇文章主要介紹了Vue EventBus自定義組件事件傳遞,組件化應(yīng)用構(gòu)建是Vue的特點(diǎn)之一,本文主要介紹EventBus進(jìn)行數(shù)據(jù)消息傳遞 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06

最新評論

桦甸市| 汪清县| 常熟市| 佛冈县| 青海省| 久治县| 桑植县| 鸡西市| 商丘市| 会宁县| 荔浦县| 台湾省| 梁平县| 马山县| 伊宁县| 抚远县| 广宗县| 资源县| 旌德县| 汝州市| 巴南区| 石嘴山市| 景洪市| 镇平县| 开原市| 定结县| 三台县| 尚志市| 蓬安县| 通城县| 中牟县| 成武县| 射洪县| 紫云| 普定县| 青阳县| 桐城市| 吉隆县| 苗栗县| 巩留县| 新竹市|