vm.$set()

vm.$set( keypath, value )

  • Arguments:

    • {String} keypath
    • {*} value
  • Usage:

    Set a data value on the Vue instance given a valid keypath. In most cases you should prefer setting properties using plain object syntax, e.g. vm.a.b = 123. This method is only needed in two scenarios:

    1. When you have a keypath string and want to dynamically set the value using that keypath.

    2. When you want to set a property that doesn’t exist.

    If the path doesn’t exist it will be recursively created and made reactive. If a new root-level reactive property is created due to a $set call, the Vue instance will be forced into a “digest cycle”, during which all its watchers are re-evaluated.

  • Example:

    var vm = new Vue({
      data: {
        a: {
          b: 1
        }
      }
    })
    
    // set an existing path
    vm.$set('a.b', 2)
    vm.a.b // -> 2
    
    // set a non-existent path, will force digest
    vm.$set('c', 3)
    vm.c // ->
  • See also: Reactivity in Depth

doc_VueJS
2016-09-25 05:48:21
Comments
Leave a Comment

Please login to continue.