您现在的位置是:网站首页> 编程资料编程资料
解读vue页面监听store值改变问题_vue.js_
2023-05-24
365人已围观
简介 解读vue页面监听store值改变问题_vue.js_
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 }以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
相关内容
- JavaScript作用域与作用域链使用重点讲解_javascript技巧_
- React18 中的 Suspense API使用实例详解_React_
- Node.js实现http请求服务与Mysql数据库操作方法详解_node.js_
- JavaScript DOM节点操作方式全面讲解_javascript技巧_
- TypeScript Typeof运算符的5个实用技巧详解_javascript技巧_
- vue 同局域网访问不到的问题及解决_vue.js_
- vue的指令和插值问题汇总_vue.js_
- 使用微信小程序制作核酸检测点查询工具_javascript技巧_
- vue3中$attrs的变化与inheritAttrs的使用详解_vue.js_
- 在 React 中使用 Redux 解决的问题小结_React_
