🌸
💫
Skip to content

Reactivity System

Create a reactive reference.

import { ref } from 'vue'
const count = ref(0)
console.log(count.value) // 0
count.value++
console.log(count.value) // 1

Create a reactive object.

import { reactive } from 'vue'
const state = reactive({
count: 0,
message: 'Hello'
})
state.count++
console.log(state.count) // 1

Create computed properties.

import { ref, computed } from 'vue'
const firstName = ref('John')
const lastName = ref('Doe')
const fullName = computed(() => {
return `${firstName.value} ${lastName.value}`
})
console.log(fullName.value) // 'John Doe'

Watch data changes.

import { ref, watch } from 'vue'
const count = ref(0)
watch(count, (newVal, oldVal) => {
console.log(`Count changed from ${oldVal} to ${newVal}`)
})
// Watch multiple sources
const firstName = ref('')
const lastName = ref('')
watch([firstName, lastName], ([newFirst, newLast], [oldFirst, oldLast]) => {
console.log('Name changed')
})
Featurerefreactive
Data TypeAny typeObject only
Access.valueDirect access
Use CasePrimitive values, objects that need reassignmentObjects, arrays