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:-
When you have a keypath string and want to dynamically set the value using that keypath.
-
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:
123456789101112131415var
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
Please login to continue.