Vue.js 3.0 Tutorial
Vue.js 3.0 Component Advanced
Vue.js 3.0 Transitions & Animations
Vue.js 3.0 Reusable & Combinable
Vue.js 3.0 Advanced
Vue.js 3.0 Tools
Vue.js 3.0 Scale
Vue.js 3.0 Accessibility
Vue.js 3.0 Migrating from Vue2
Vue.js 3.0 Contribute Documentation
Vue.js 3.0 API References
Vue.js 3.0 Style Guide
In Vue 3's Composition API, one of the key reactive primitives is the ref
function. ref
is used to create a reactive reference to a value.
Here's a basic usage of ref
:
import { ref } from 'vue' export default { setup() { const count = ref(0) return { count } } }
In this example, count
is a reactive reference to the value 0
.
Within the setup
method, you must access the value of ref
by using .value
:
export default { setup() { const count = ref(0) function increment() { count.value++ } return { count, increment } } }
However, when a ref
is returned from setup
and used within the template, Vue automatically unwraps it and you can use it like a normal value:
<template> <button @click="increment">{{ count }}</button> </template>
In this example, you can see count
being used directly in the template, even though it's a ref
.
It's important to remember that ref
creates a reactive reference, not a reactive value. The reference is what's reactive, not the value itself. This means that if you reassign the reference to another value, it won't update the original ref
.
When you need to manage a primitive value (like a string, number, or boolean) or when you want a reactive property inside an object, ref
is a great tool to use.
Remember that Vue's reactivity system is built around these concepts, so understanding how ref
and reactive
work is essential for effective Vue development.
Introduction to Vue 3.0 ref
for Reactive Data:
ref
creates a reactive reference for primitive values.import { ref } from 'vue'; const count = ref(0);
Vue 3.0 ref
vs reactive
for State Management:
ref
for primitive values (numbers, strings, etc.).reactive
for complex objects.import { ref, reactive } from 'vue'; const count = ref(0); const state = reactive({ message: 'Hello, Vue 3.0!', });
Vue 3.0 Creating and Updating ref
s:
ref
with an initial value..value
property.import { ref } from 'vue'; const count = ref(0); // Update the value count.value++;
Vue 3.0 Multiple ref
s in a Component:
ref
s in a component.import { ref } from 'vue'; const count = ref(0); const message = ref('Hello, Vue 3.0!');
Vue 3.0 Accessing the Value of a ref
:
ref
using the .value
property.import { ref } from 'vue'; const count = ref(0); // Access the value const currentCount = count.value;
Vue 3.0 Reactive Data with the ref
Function:
ref
function to create reactive references.import { ref } from 'vue'; const count = ref(0);
Vue 3.0 Modifying ref
s with the .value
Property:
ref
using the .value
property.import { ref } from 'vue'; const count = ref(0); // Modify the value count.value = 10;
Vue 3.0 Watching ref
s with the watch
Function:
ref
using the watch
function.import { ref, watch } from 'vue'; const count = ref(0); watch(count, (newValue, oldValue) => { console.log(`Count changed from ${oldValue} to ${newValue}`); });
Vue 3.0 ref
Objects and Reactivity:
ref
objects are reactive and trigger updates.import { ref } from 'vue'; const count = ref(0); // Changes to count will trigger updates count.value++;
Vue 3.0 Using ref
s in Templates:
ref
directly in templates.<template> <p>{{ count }}</p> </template> <script> import { ref } from 'vue'; export default { setup() { const count = ref(0); return { count, }; }, }; </script>
Vue 3.0 Handling Reactivity Caveats with ref
:
import { ref } from 'vue'; const obj = ref({ count: 0, }); // Incorrect: Adding a non-reactive property obj.value.newProperty = 'Not reactive';
Vue 3.0 ref
s and TypeScript:
ref
.import { ref, Ref } from 'vue'; const count: Ref<number> = ref(0);