Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Vue.set() and Vue.use() in .vue files

Tags:

vue.js

vuejs2

How can I use Vue.set() and Vue.use() in .vue files? I'm using the vue-cli to scaffold my project and I need to use Vue.use(VeeValidate) for validation. Also I would like to use something like following, found here.

Vue.set(example1.items, indexOfItem, newValue)

Since the .vue files export object, How do I get the Vue reference. Also I would like to use it inside my default object exported in .vue files. What I mean is that I need to use Vue.set() on an item present in my data function.

like image 251
donnyjeremiah Avatar asked Dec 10 '16 14:12

donnyjeremiah


People also ask

What is the use of Vue set?

Vue. set is a tool that allows us to add a new property to an already reactive object, and makes sure that this new property is ALSO reactive.

What is defineComponent in vue3?

defineComponent({ setup: function , name: 'some name' }); As you see, all these cases are responsible for different scenarios, yet the return type is always the same: a defineComponent interface with all the types you need to apply for the props, data and the other settings that are needed for the component.

What are .Vue files?

Vue Single-File Components (a.k.a. *.vue files, abbreviated as SFC) is a special file format that allows us to encapsulate the template, logic, and styling of a Vue component in a single file.

How does Vue work under the hood?

Under the hood Vue will walk through all the properties that we define into the data and converts them to getter/setters using Object. defineProperty. When any data property gets a new value then the set function will notify the Watchers. A Watcher is created for each component when a Vue application is initialized.


1 Answers

Your components have Vue.set available as this.$set. For reference: instance methods & properties.

Vue.use is a global method and can be called wherever. It is basically never called inside a component(and might actually throw if you try? Never tried it). Call it wherever you're doing the rest of your initialization.

like image 174
Alex Sakharov Avatar answered Sep 27 '22 15:09

Alex Sakharov