0

I am trying to learn vue . I grabbed a demo project and have much of it working but I am not sure I under stand what this does.

 const search = ref("");
 const movies= ref([]);

in the template and other places it then accesses the value using search.value. Why use Ref("") ?

2 Answers 2

1

ref() allows us to create a "reference" to any value and pass it around without losing reactivity.

The beauty behind that is the fact that you can create reactive properties outside your Vue component. Inside a plain js (or ts) file, just import "ref" from the "vue" package, and then create a reactive property, e.g.

const msg = ref("hello")

For the examples you provided, you're simply initializing the variables with an empty string for search and empty array for movies.

Sign up to request clarification or add additional context in comments.

Comments

1

In Vue 3, ref() makes a variable reactive.

https://vuejs.org/api/reactivity-core.html#ref

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.