vue设置属性没响应(Vue.$set 失效的坑 问题发现及解决方案)
类别:编程学习 浏览量:539
时间:2021-10-13 00:51:50 vue设置属性没响应
Vue.$set 失效的坑 问题发现及解决方案偶然在项目中发现Vue.$set失效
有这样一个需求 添加数据过滤用 左边是控件选择 中间是条件 右边是值
因为会根据控件不同渲染不同的值选项控件
<el-form inline > <el-form-item style="margin-bottom: 20px;"> <el-select v-model="data[props.prop]" @change="data[props.value] = ''"> <el-option v-for="item in controls" :key="item.id" :value="item.id" :label="item.label"> </el-option> </el-select> </el-form-item> <el-form-item style="margin-bottom: 20px;"> <el-select v-model="data[props.type]"> <el-option v-for="item in condition" :key="item.code" :value="item.code" :label="item.name" ></el-option> </el-select> </el-form-item> <el-form-item style="margin-bottom: 20px;"> <FormControl v-if="control" :control="control" :value="data[props.value]" @input="onValueChange" ></FormControl> <el-input v-else :value="data[props.value]" @input="onValueChange"></el-input> </el-form-item> <el-form-item> <el-button type="primary" icon="el-icon-plus" @click="add"></el-button> </el-form-item> </el-form>
{ props:{ props: { type: Object, default: () => ({ prop: 'prop', value: 'value', type: 'type' }) } }, data(){ return { data:{ } } }, methods:{ onValueChange(val){ this.$set(this.data, this.props.value, val) } } //... }
由于控件ID的不确定性 所有 data并不能提前预设好key 自然无法响应 所以在onValueChange 使用了this.$set动态添加数据实现响应
复现可以发现 值输入框内的数据并不能实时响应
明明用了$set却不能响应 一番排查后发现只要切换控件后 value值就不能响应 但是只要在切换前随便输入点啥 再切换就没问题
又是一番排查后发现
<el-select v-model="data[props.prop]" @change="data[props.value] = ''">
删除@change事件后故障解决
问题出现在 data[props.value] = ''
遂查看Vue源码
//vue/src/core/observer/index.js 源码片段 /** * Set a property on an object. Adds the new property and * triggers change notification if the property doesn't * already exist. */ export function set (target: Array<any> | Object, key: any, val: any): any { if (process.env.NODE_ENV !== 'production' && (isUndef(target) || isPrimitive(target)) ) { warn(`Cannot set reactive property on undefined, null, or primitive value: ${(target: any)}`) } if (Array.isArray(target) && isValidArrayIndex(key)) { target.length = Math.max(target.length, key) target.splice(key, 1, val) return val } if (key in target && !(key in Object.prototype)) { target[key] = val return val } const ob = (target: any).__ob__ if (target._isVue || (ob && ob.vmCount)) { process.env.NODE_ENV !== 'production' && warn( 'Avoid adding reactive properties to a Vue instance or its root $data ' + 'at runtime - declare it upfront in the data option.' ) return val } if (!ob) { target[key] = val return val } defineReactive(ob.value, key, val) ob.dep.notify() return val }
可以发现 在defineReactive之前 判断了key是否存在于对象之内 若存在就跳过了
坑就在这 多次翻阅Vue.$set文档并未发现$set不能为已存在的key添加监测对象
删除 data[props.value] = '' 改为 onValueChange('') 完美解决问题
总结
Vue.$set之前一定要对象内key不存在 不然只会更新值 并不会为该Key添加响应监测
以上为个人经验,希望能给大家一个参考,也希望大家多多支持开心学习网。
您可能感兴趣
- vuekeep-alive源码(vue中keepAlive组件的作用和使用方法详解)
- vue滚动条滚动事件(vue实现一个滚动条样式)
- vue3.0全家桶教程elementui学习(vite+vue3.0+ts+element-plus快速搭建项目的实现)
- vue应用转flutter(Vue和Flask通信的实现)
- vue中的ref(Vue3.0中Ref与Reactive的区别示例详析)
- vue 富文本图片上传(vue.js云存储实现图片上传功能)
- vue代码和element用法(Vue Element前端应用开发之整合ABP框架的前端登录)
- vue如何加速(vue函数防抖与节流的正确使用方法)
- vue集成文件上传插件(vue 实现上传组件)
- vue前端打包发布教程(Vue项目打包、合并及压缩优化网页响应速度)
- vue和springboot实战项目(vue+spring boot实现校验码功能)
- vue怎么操作表格(如何在在Vue3中使用markdown 编辑器组件)
- vue动态列表布局(vue项目实现左滑删除功能完整代码)
- vue考试链接重置(Vue实现答题功能)
- vue怎么在中间加图片(Vue实现多图添加显示和删除)
- vue怎么实现拖动(Vue拖动截图功能的简单实现方法)
- 盘古开天地 他创造了世界,谁创造了盘古 盘古是伏羲吗(盘古开天地他创造了世界)
- 关于队徽 你了解这些么 二(关于队徽你了解这些么)
- 冬天来了手脚冰凉 真不是因为上辈子你是折翼的天使(冬天来了手脚冰凉)
- 0 1 岁婴儿最强作息指南,照着做养出天使宝宝(01岁婴儿最强作息指南)
- 沪上这16所高校 萌新 礼包开箱 哪一款让你心动(沪上这16所高校萌新)
- 她救了被绑架的他,而这一切竟是一场阴谋...(她救了被绑架的他)