I have create a global object in my main.js to access logged in user's data throughout the application between controllers
main.js
Vue.prototype.user = getUser() // assume a method to getUser data
Now the above .user
object will be available in other components with this.user
,
what I'm looking for is if I make change to this.user
in any component, the changes reflects to other components too, this is not happening right now
I've two components, one to update data in this.user
EditProfileComponent.vue
<input type="text" v-model="user.firstName" />
<input type="text" v-model="user.lastName" />
and other to show data UserCardComponent.vue
<h1>{{user.firstName}} {{user.lastName}}<h1>
on the submit of above form I'm saving data into database and getting it back at with getUser()
on refresh,
What I'm expecting, when I make change to v-model in form it should show change directly into UserCardComponent.vue
When you set the value of
Vue.prototype.user
, Vue is not expecting that value to be bound to the DOM, so it won't be reactive. You maybe have seen global services set up this way (likeVue.prototype.$http
or similar), but these objects are not being bound directly to the DOM.The most straight-forward way to maintain a reactive globally-accessible object is to use Vuex.
You can create a store to contain the
user
object:And register the store by passing the
store
object to the root Vue instance:Then, in whichever components you need to access the
user
object, you can define a computeduser
property via theVuex.mapState
help function:Here's a simple example:
If you don't want to have to add the Vuex library, (and you really don't mind each of your components having access to the
user
object) you could also use a mixin:This way, all of your Vue instances will have access to a reactive
user
object pointing to theuser
object initially set in youmain.js
file.Here's an example of that: