Skip to content
Advertisement

Vuejs – cannot access $root data from component

I’m using vue.js to manage the user and I have this piece of code to initialize vue:

JavaScript

Inside store I have:

JavaScript

In this way, the user is correctly initialized.

Now, I have also a vue component with the following:

JavaScript

But the console.log(this.$root.store.user.state.user) inside the init() method results in null, while the one inside computed returns the actual user.

Why it’s like this? Shouldn’t the init() method returns the user anyway?

Thank you all.

Advertisement

Answer

If you are able to change your init() method to return Promise – then you can wait for that Promise to resolve before instantiating the whole Vue application:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement