Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best approach to change variable value outside of Vue component

I am searching for the best approach to change Vue variable value outside of component. I'm using Vue webpack as well.

I have created a project using vue webpack. Inside its default App.vue file, I have a variable. For example, let's take showModal and its default value is false. Then I built it in a single javascript file.

<button>Register</button> {{-- event button --}}
<div id="guest"></div> {{-- Here I'm rendering my template --}}

<script src="{{ asset('js/vue-js/guest.js') }}"></script> {{-- builded Javascript file --}}

And the problem is that I want to change my showModal variable to true, but the event button it is not on my component template.

What is the best approach to accomplish this?

like image 205
bakis Avatar asked Jul 04 '18 12:07

bakis


People also ask

What is the point of reactivity in Vue?

Vue's reactivity system works by deeply converting plain JavaScript objects into reactive proxies. The deep conversion can be unnecessary or sometimes unwanted when integrating with external state management systems (e.g. if an external solution also uses Proxies).

How do I make my house reactive in Vue?

The Reactive Method #observable() in Vue 2.6, can be useful when we're trying to create an object all of whose properties are reactive (such as the data object in the Options API). Under the hood, the data object in the Options API uses this method to make all of the properties in it reactive.

Are methods reactive Vue?

No, methods are not reactive. Only data can be reactive in Vue.


1 Answers

If you want to access a vue component outside of vue you could register it to the window object and access it then from anywhere.

window.myVueComponent = new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

Now you can access it from anywhere else with

window.myVueComponent.myValue = 123

But this "style" is called global namespace pollution for reasons. ;)

I think it is better to extend your vue app so that the button is also within the vue-handled components.

like image 56
Thomas Avatar answered Sep 18 '22 13:09

Thomas