解读vue页面监听store值改变问题

 更新时间:2022年10月24日 09:09:56   作者:榴莲豆包  
这篇文章主要介绍了解读vue页面监听store值改变问题,具有很好的参考价值,希望对大家有所帮助。

vue页面监听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,
  }
}

重点是:

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;
 
        },
 
    },

使用深度监听新值变化:

watch:{     //监听value的变化,进行相应的操做便可
    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, // 深度监听
        immediate: true,//立即执行
    },
  },

完毕,这样能解决绝大部分问题。 

vue监听store.state对象变化不起作用

store.state中的对象属性发生改变,引用state的组件中却监听不到变化,深度监听也不起作用,如下代码:

// 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) {
     // 问题就出现在此处
    state.noticeParams = data
  }

由于重新赋值的代码中里引用了初始 state.noticeParams,而mutations的方法中将改变后的state.noticeParams又重新赋值给state.noticeParams,此时state.noticeParams里的属性值发生了改变但引用并未发生变化,所以监听没起作用,解决方法就是创建新的数组

setNoticeParams(state, data) {
    let arr = Object.assign([], state.noticeParams, data);
    state.noticeParams = arr
    // 创建一个空数组,将初始state.noticeParams与改变后的state.noticeParams==》data合并,最后赋值给state.noticeParams
  }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Vue二次封装axios流程详解

    Vue二次封装axios流程详解

    在vue项目中,和后台交互获取数据这块,我们通常使用的是axios库,它是基于promise的http库,下面这篇文章主要给大家介绍了关于Vue3引入axios封装接口的两种方法,需要的朋友可以参考下
    2022-10-10
  • Element Breadcrumb 面包屑的使用方法

    Element Breadcrumb 面包屑的使用方法

    这篇文章主要介绍了Element Breadcrumb 面包屑的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • vuecli+AXdownload下载组件封装 +css3下载悬浮球动画效果

    vuecli+AXdownload下载组件封装 +css3下载悬浮球动画效果

    当触发下载功能的时候,会触发一个下载动画,下载悬浮球会自动弹出,并且闪烁提示有新的下载任务直到下载任务完场提示,接下来通过本文介绍vuecli+AXdownload下载组件封装 +css3下载悬浮球动画效果,需要的朋友可以参考下
    2024-05-05
  • vue2实现简易时钟效果

    vue2实现简易时钟效果

    这篇文章主要为大家详细介绍了vue2实现简易时钟效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • Vue中的$set的使用实例代码

    Vue中的$set的使用实例代码

    这篇文章主要介绍了Vue中的$set的使用,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-10-10
  • vue App.vue中的公共组件改变值触发其他组件或.vue页面监听

    vue App.vue中的公共组件改变值触发其他组件或.vue页面监听

    这篇文章主要介绍了vue App.vue里的公共组件改变值触发其他组件或.vue页面监听,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-05-05
  • vue axios请求成功却进入catch的原因分析

    vue axios请求成功却进入catch的原因分析

    这篇文章主要介绍了vue axios请求成功却进入catch的原因分析,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • 浅谈axios中取消请求及阻止重复请求的方法

    浅谈axios中取消请求及阻止重复请求的方法

    在实际项目中,我们可能需要对请求进行“防抖”处理。本文主要实现axios中取消请求及阻止重复请求,具有一定的参考价值,感兴趣的可以了解一下
    2021-08-08
  • vue.js整合mint-ui里的轮播图实例代码

    vue.js整合mint-ui里的轮播图实例代码

    这篇文章主要介绍了vue.js整合mint-ui里的轮播图的方法,首先我们需要初始化vue项目,然后安装mint-ui。具体内容详情大家通过学习
    2017-12-12
  • Vue EventBus自定义组件事件传递

    Vue EventBus自定义组件事件传递

    这篇文章主要介绍了Vue EventBus自定义组件事件传递,组件化应用构建是Vue的特点之一,本文主要介绍EventBus进行数据消息传递 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06

最新评论