美文网首页Vue技术前端开发那些事儿
Vue.set()和this.$set()的使用

Vue.set()和this.$set()的使用

作者: Bonne_nuit | 来源:发表于2021-04-30 14:55 被阅读0次

Vue.set()和this.$set()介绍:

在我们使用vue进行开发的过程中,可能会遇到一种情况:当生成vue实例后,当再次给数据赋值时,有时候并不会自动更新到视图上去;
当我们去看vue文档的时候,会发现有这么一句话:如果在实例创建之后添加新的属性到实例上,它不会触发视图更新。

如下代码,给 student对象新增 age 属性

data () {
  return {
    student: {
      name: '',
      sex: ''
    }
  }
}
mounted () { // ——钩子函数,实例挂载之后
  this.student.age = 24
}

受 ES5 的限制,Vue.js 不能检测到对象属性的添加或删除。因为 Vue.js 在初始化实例时将属性转为 getter/setter,所以属性必须在 data 对象上才能让 Vue.js 转换它,才能让它是响应的。

正确写法:this.$set(this.data,”key”,value')

mounted () {
  this.$set(this.student,"age", 24)
}

:: Vue 不允许动态添加根级响应式属性。

例如:

const app = new Vue({
  data: {
    a: 1
  }
  // render: h => h(Suduko)
}).$mount('#app1')

Vue.set(app.data, 'b', 2)

只可以使用 Vue.set(object, propertyName, value) 方法向嵌套对象添加响应式属性,例如

var vm=new Vue({
    el:'#test',
    data:{
        //data中已经存在info根属性
        info:{
            name:'小明';
        }
    }
});

//给info添加一个性别属性

Vue.set(vm.info,'sex','男');

Vue.set()和this.$set()实现原理

我们先来看看Vue.set()的源码:

import { set } from '../observer/index'

...
Vue.set = set
...
再来看看this.$set()的源码:

import { set } from '../observer/index'

...
Vue.prototype.$set = set

...
结果我们发现Vue.set()和this.set()这两个api的实现原理基本一模一样,都是使用了set函数。set函数是从 ../observer/index 文件中导出的,区别在于Vue.set()是将set函数绑定在Vue构造函数上,this.set()是将set函数绑定在Vue原型上。

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
}

我们发现set函数接收三个参数分别为 target、key、val,其中target的值为数组或者对象,这正好和官网给出的调用Vue.set()方法时传入的参数参数对应上。
转载:https://juejin.cn/post/6844903901175496711

相关文章

  • vue强制更新

    使用update this.$forceUpdate(),强制视图更新 用vue.set this.$set(th...

  • Vue.set()实现数据动态响应

    this.$set()和Vue.set()本质方法一样,前者可以用在methods中使用。 在vue里面,我们操作...

  • Vue.set()实现数据动态响应!!!

    this.$set()和Vue.set()本质方法一样,前者可以用在methods中使用。 在vue里面,我们操作...

  • Vue.set()和this.$set()的使用

    Vue.set()和this.$set()介绍: 在我们使用vue进行开发的过程中,可能会遇到一种情况:当生成vu...

  • Vue.set()和this.$set()

    在我们使用vue进行开发的过程中,可能会遇到一种情况:当生成vue实例后,当再次给数据赋值时,有时候并不会自动更新...

  • Vue.set() 和this.$set()

    这两个方法其实是差不多的,可以说几乎是一样的,只不过set是绑定在Vue构造函数上,$set()是绑定在Vue原型...

  • Vue.set和Vue数据监测

    Vue.set this.$set(this.student,'添加的属性','添加的属性值') Vue数据监测 ...

  • 深入理解Vue的数据响应式

    【目录】一、Vue对data做了什么?二、数据响应式三、Vue.set和this.$set 【正文】在学习Vue的...

  • vue.set/this.$set

    vue项目中,几个表格分开展示,但是展示的内容都一样(不展示的内容有区别),为了省事儿,就给封装了个子组件,但是在...

  • Vue.set()和this.$set()区别

    Vue.set()的源码import { set } from '../observer/index'...Vue...

网友评论

    本文标题:Vue.set()和this.$set()的使用

    本文链接:https://www.haomeiwen.com/subject/sdbnrltx.html