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) // 1reactive
Section titled “reactive”Create a reactive object.
import { reactive } from 'vue'
const state = reactive({ count: 0, message: 'Hello'})
state.count++console.log(state.count) // 1computed
Section titled “computed”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 sourcesconst firstName = ref('')const lastName = ref('')
watch([firstName, lastName], ([newFirst, newLast], [oldFirst, oldLast]) => { console.log('Name changed')})Comparison of ref and reactive
Section titled “Comparison of ref and reactive”| Feature | ref | reactive |
|---|---|---|
| Data Type | Any type | Object only |
| Access | .value | Direct access |
| Use Case | Primitive values, objects that need reassignment | Objects, arrays |