解讀vue頁(yè)面監(jiān)聽(tīng)store值改變問(wèn)題
vue頁(yè)面監(jiān)聽(tīng)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: {
//聊天消息儲(chǔ)存
SET_MESSAGELIST:(state, info)=>{
let data = Object.assign([], state.messageList ,info)
state.messageList = data
},
},
actions: {
},
// 同步緩存頁(yè)面
AsyncIncludes ({ commit, state }, data) {
commit('SET_INCLUDES', data)
},
// 新增緩存頁(yè)面
AddIncludes ({ commit, state }, path) {
let includes = state.includes
if (includes.indexOf(path) < 0) {
includes.push(path)
}
commit('SET_INCLUDES', includes)
},
// 刪除緩存頁(yè)面
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計(jì)算屬性取值:
?computed: {
?
? ? ? ? cuserList() {
?
? ? ? ? ? ? return this.$store.state.permission.messageList;
?
? ? ? ? },
?
? ? },使用深度監(jiān)聽(tīng)新值變化:
watch:{ ? ? //監(jiān)聽(tīng)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)聽(tīng)
? ? ? ? immediate: true,//立即執(zhí)行
? ? },
? },完畢,這樣能解決絕大部分問(wèn)題。
vue監(jiān)聽(tīng)store.state對(duì)象變化不起作用
store.state中的對(duì)象屬性發(fā)生改變,引用state的組件中卻監(jiān)聽(tīng)不到變化,深度監(jiān)聽(tīng)也不起作用,如下代碼:
// 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) {
??? ?// 問(wèn)題就出現(xiàn)在此處
? ? state.noticeParams = data
? }由于重新賦值的代碼中里引用了初始 state.noticeParams,而mutations的方法中將改變后的state.noticeParams又重新賦值給state.noticeParams,此時(shí)state.noticeParams里的屬性值發(fā)生了改變但引用并未發(fā)生變化,所以監(jiān)聽(tīng)沒(méi)起作用,解決方法就是創(chuàng)建新的數(shù)組
setNoticeParams(state, data) {
? ? let arr = Object.assign([], state.noticeParams, data);
? ? state.noticeParams = arr
? ? // 創(chuàng)建一個(gè)空數(shù)組,將初始state.noticeParams與改變后的state.noticeParams==》data合并,最后賦值給state.noticeParams
? }以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vuecli+AXdownload下載組件封裝?+css3下載懸浮球動(dòng)畫(huà)效果
當(dāng)觸發(fā)下載功能的時(shí)候,會(huì)觸發(fā)一個(gè)下載動(dòng)畫(huà),下載懸浮球會(huì)自動(dòng)彈出,并且閃爍提示有新的下載任務(wù)直到下載任務(wù)完場(chǎng)提示,接下來(lái)通過(guò)本文介紹vuecli+AXdownload下載組件封裝?+css3下載懸浮球動(dòng)畫(huà)效果,需要的朋友可以參考下2024-05-05
vue2實(shí)現(xiàn)簡(jiǎn)易時(shí)鐘效果
這篇文章主要為大家詳細(xì)介紹了vue2實(shí)現(xiàn)簡(jiǎn)易時(shí)鐘效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
vue App.vue中的公共組件改變值觸發(fā)其他組件或.vue頁(yè)面監(jiān)聽(tīng)
這篇文章主要介紹了vue App.vue里的公共組件改變值觸發(fā)其他組件或.vue頁(yè)面監(jiān)聽(tīng),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
vue axios請(qǐng)求成功卻進(jìn)入catch的原因分析
這篇文章主要介紹了vue axios請(qǐng)求成功卻進(jìn)入catch的原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
淺談axios中取消請(qǐng)求及阻止重復(fù)請(qǐng)求的方法
在實(shí)際項(xiàng)目中,我們可能需要對(duì)請(qǐng)求進(jìn)行“防抖”處理。本文主要實(shí)現(xiàn)axios中取消請(qǐng)求及阻止重復(fù)請(qǐng)求,具有一定的參考價(jià)值,感興趣的可以了解一下2021-08-08

