Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
up vote 1 down vote accepted

You have to use import wherever you need Vue or vee-validate, You can do it like following inside script tag:

<script>
import Vue from 'vue'
import VeeValidate from 'vee-validate'

Vue.use(VeeValidate)
...
...
</script>

If you just want to do Vue.set, you can instead do this.$set wcich is the alias of the global Vue.set.

share|improve this answer
    
So I can do import Vue from 'vue' and ` import VeeValidate from 'vee-validate'` from any child component and use validation there? Or should I only use this in my main.js file? – donnyjeremiah Dec 10 '16 at 18:12
    
@donnyjeremiah You can do it either way, both are equivalent, but if this is being used in multiple components, than do it in main.js file. – saurabh Dec 11 '16 at 2:43

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.

share|improve this answer
    
Is there any alias for Vue.use() like this.$set is for Vue.set(), so I can use it in the component I wish to use validation for? Because I don't want to use Vue.use(VeeValidate) in my main.js file where I initialize App.vue. I want it only for a certain component. – donnyjeremiah Dec 10 '16 at 18:09
    
It is a plugin. It will load and install globally, even if imported and use()'d in just one component. – Alex Sakharov Dec 10 '16 at 19:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.