vue的$set源码
2022/1/10 17:06:22
本文主要是介绍vue的$set源码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
function set(target: Array<any> | Object, key: any, val: any): any { // isUndef 是判断 target 是不是等于 undefined 或者 null 。 //isPrimitive 是判断 target 的数据类型是不是 string、number、symbol、boolean 中的一种 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 } // vue给响应式对象(比如 data 里定义的对象)都加了一个 __ob__ 属性, // 如果一个对象有这个 __ob__ 属性,那么就说明这个对象是响应式对象,我们修改对象已有属性的时候就会触发页面渲染。 // 非 data 里定义的就不是响应式对象。 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 }
这篇关于vue的$set源码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-09vue3开发前端表单缓存自定义指令,移动端h5必备插件
- 2024-05-09React Hooks在class组件中的使用方式
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode
- 2024-03-13vuex-persist
- 2024-03-11icons for vue
- 2024-03-07breadcrumbs react js
- 2024-03-06react login page example
- 2024-03-06react router uselocation
- 2024-03-04postgres jsonb_set