Vue3 watch Property
In Vue 3,watch The property is used to watch changes to a certain piece of data in a Vue instance and execute the corresponding callback function when the data changes.
watch allows us to observe and respond to changes in specific data within a Vue instance, thereby performing side effects such as updating the DOM or making network requests.
2. Basic Usage of the watch Property
In Vue 3,watch The property can be used in the following way:
Example
import
{
ref
,
watch
}
from
'vue'
;
export
default
{
setup
(
)
{
const
count
=
ref
(
0
)
;
watch
(
count
,
(
newValue
,
oldValue
)
=>
{
console.
log
(
`count changed from $
{
oldValue
}
to $
{
newValue
}
`
)
;
}
)
;
return
{
count
}
;
}
}
;
3. Code Explanation
In the code above:
- First, we use
refcreates a reactive datacountand set its initial value to 0. - Then, we use the
watchfunction to watchcountfor changes.watchaccepts two parameters: - The first parameter is the data we want to watch (here it is
count). - The second parameter is a callback function that is triggered when the watched data changes. The callback function receives two parameters:
newValue: The new value after the data changes.oldValue: The old value before the data changes.
newValue: The new value after the data changes.oldValue: The old value before the data changes.
4. Advanced Usage of the watch Property
In addition to watching a single piece of data,watch can also watch multiple pieces of data, or watch a complex expression.
4.1 Watching Multiple Data
Example
import
{
ref
,
watch
}
from
'vue'
;
export
default
{
setup
(
)
{
const
firstName
=
ref
(
'John'
)
;
const
lastName
=
ref
(
'Doe'
)
;
watch
(
[
firstName
,
lastName
]
,
(
[
newFirstName
,
newLastName
]
,
[
oldFirstName
,
oldLastName
]
)
=>
{
console.
log
(
`firstName changed from $
{
oldFirstName
}
to $
{
newFirstName
}
`
)
;
console.
log
(
`lastName changed from $
{
oldLastName
}
to $
{
newLastName
}
`
)
;
}
)
;
return
{
firstName
,
lastName
}
;
}
}
;
In this example, we watched firstName and lastName the changes of two pieces of data. When any of them changes, the callback function is triggered.
4.2 Watching Complex Expressions
Example
import
{
ref
,
watch
}
from
'vue'
;
export
default
{
setup
(
)
{
const
user
=
ref
(
{
name
:
'Alice'
,
age
:
25
}
)
;
watch
(
(
)
=>
user.
value
.
age
,
// Watch changes to user.age
(
newAge
,
oldAge
)
=>
{
console.
log
(
`age changed from $
{
oldAge
}
to $
{
newAge
}
`
)
;
}
)
;
return
{
user
}
;
}
}
;
In this example, we watch user.age by using a function. This approach is suitable for watching a property inside an object.
5. Options of the watch Property
watch The function also accepts a third parameter, an optional configuration object used to customize the watching behavior.
Example
import
{
ref
,
watch
}
from
'vue'
;
export
default
{
setup
(
)
{
const
count
=
ref
(
0
)
;
watch
(
count
,
(
newValue
,
oldValue
)
=>
{
console.
log
(
`count changed from $
{
oldValue
}
to $
{
newValue
}
`
)
;
}
,
{
immediate
:
true
,
// Execute the callback immediately
deep
:
true
// Deep watching, suitable for watching objects or arrays
}
)
;
return
{
count
}
;
}
}
;
immediate: true: Indicates that the callback function is executed immediately when watching starts, rather than waiting for the data to change.deep: true: Indicates deep watching, suitable for watching changes to objects or arrays.
Vue3 Options API