Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js access parent/root data from a child component

My main.js file references #app as the el, and then on ready it sends an HTTP request to my API to get data on the user based on their token taken from localStorage.

It sets the data like so when the response is returned:

this.$http.get('/me', function(data) {
  this.$set('me', data.me);
})

After the above request returns a response, data now looks like this:

data = {
  me: {
    email: '[email protected]',
    name: 'Email Email'
  }
}

And then in the main.js file I implement vue-router and my app is then bootstrapped. It renders my side navigation and other components, and in the middle there is a <router-view></router-view> for the main content.

Ex (app.html rendered from main.js):

<div is="navigation"></div>
<router-view></router-view>

In my navigation component, and furthermore my components rendered by the vue-router, how can I access data that I want to be global, for example me which is set in the root component?

I want to be able to access data like the user's e-mail and name all over the app.

like image 839
Noah Avatar asked Dec 06 '15 20:12

Noah


People also ask

How do you access a root component from a child instance in Vue?

We can access the root instance of from a child component with the $root property. The code above created a rootFoo computed property from the root Vue instance's foo component and displayed it inside the foo component. So we get foo displayed.

How do you emit data from child to parent?

Prepare Child component to emit data Furthermore, we need to know that the child component uses the @Output() property to raise an event (by using an EventEmitter) to notify the parent of the change. @Output() is a decorator that marks a class field (that has to be named) as an output property.

How do I transfer data from component to component in Vue?

VueJS props are the simplest way to share data between components. Props are custom attributes that we can give to a component. Then, in our template, we can give those attributes values and — BAM — we're passing data from a parent to a child component!


2 Answers

EDIT:: Check out the new package Vuex - https://github.com/vuejs/vuex . This is designed to be a high-level storage of app data that you can access from within any component. This is definitely the best option now.

Original answer:


A few options that I can think of:

1) pass the current user data to each component that needs it. This would be a bit annoying but maintains the principle that a component should not need any outside information to perform its function.

2) use this.$parent to move up the component chain to the root component, where you will be able to access the data you need.

3) just make a global variable. Declare it outside the scope of Vue and have access to it wherever you want.

like image 173
Jeff Avatar answered Sep 30 '22 23:09

Jeff


$root works for me. At each level of components hierarchy, it refers to the root data.

For example you are inside a component and a sub component needs email, you can do this to access the email:

$root.me.email

like image 34
AliN11 Avatar answered Sep 30 '22 22:09

AliN11