0

First of all, it sounds like this situation is perfect of use eventBus but I am just experimenting. What I feel this will element the process of emitting and capturing event.

Say that in my main.js I have declared:

Vue.prototype.$someHeight = 200

in some of my component I tried to change it to:

this.$someHeight = 300

But when I use it in my vue, it still says: {{ $someHeight }} // output: 200, and I was expecting 300

So, how to override it? Is this a good practice?

1
  • This is something you normally do in a vuex store. Commented Dec 23, 2018 at 9:32

1 Answer 1

4

Every vue component is an instance of Vue. When you define a variable (such as $someHeight) on Vue's prototype, all new components created will get their own copy of such variable.

Which means every Vue component will have its own copy of $someHeight with value of 200. Now even if you set its value to 300, all other components will have the old value of 200.

It is not a good practice. You should only define functions on prototype: Vue doc

Now you can either use Vuex for this or create a global vue instance and use it to store your global variables. Vuex recommended of-course!

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

3 Comments

application is too small size, i can't use vuex for such a small thing. what should we need to do except vuex
Then just declare a vue instance and use it everywhere.
could you give me an example please? using code

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.