1

I have the following code in my app.js (after importing Vue and before new Vue()):

Vue.prototype.$globalArray = [];

And in one of my Vue components I have the following:

<span v-for="item in $globalArray">{{ item }}</span>

The problem then is that when I do $vm0.$globalArray.push('some text'); in my Chrome dev tools (with the Vue extension hence the $vm0), nothing happens. However when I change my app.js to

Vue.prototype.$globalArray = 'some other text';

It does actually show the 'some other text' so it looks like it only renders the initial value but doesn't update. The same goes for when I set a timeout of a couple of seconds after the component has mounted and then insert something in the $globalArray.

Could someone tell me whether I'm doing something wrong or that this is not supported?

Thanks in advance

1 Answer 1

3

You are defining just a global vue instance and it isn't reactive by default - https://v2.vuejs.org/v2/cookbook/adding-instance-properties.html

To make it reactive you can follow Vue.observable for example - https://v2.vuejs.org/v2/api/#Vue-observable

Vue.prototype.$global = Vue.observable({array: []})
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer but it did not work for me directly. What did work for me however, is Vue.observable({array: []}) and then render and push to $globalArray.array. Is that what you actually meant? Or should your exact answer also work and am I doing something wrong?

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.