0

How do I get a boolean authenticated on the main instance in vue.js 2.0?

Right now I've got this:

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

Then in my App component:

data() {
    return {
        authenticated: false
    }
}

But it's not set on the Root component. So I cannot access it through:

{{ this.$root.authenticated }}

In my childcomponents (for example Navigation). How can I fix this. In vue.js 1.0 this was working!

enter image description here

12
  • Maybe it's better to handle this with one separated file and vue-router - Check this github.com/TahaSh/spa-forum/blob/master/resources/assets/js/… even if this is vue 1 repo. Commented Dec 13, 2016 at 14:36
  • Thanks for your reply. But then I still cannot show for example the user name that's on a user object on my main app.vue instance in the navbar. I really need to solve this. Commented Dec 13, 2016 at 14:38
  • What you have in your root instance ? If you want to send user name to Navigation component, you can send it from anonymous component as prop, and just display it. Make sense ? I think you don't need the authenticated property in Root instance. Commented Dec 13, 2016 at 14:41
  • Yes, I understand. Sometimes I've got a component within a component. So then I've to pass a prop from one component to the other and that component will pass it to another component. That's not how I would like to do it :) Commented Dec 13, 2016 at 14:43
  • I don't really get the point.Even if you have this data in Root instance, you still have to pass it down as prop to another component (for e.g navigation) if you want use it here. Commented Dec 13, 2016 at 14:45

1 Answer 1

1

You could try to define data object on root Vue instance.Like this:

const app = new Vue({
    data: {
      msg: 'foo'
    },
    router,
    render: h => h(App)
}).$mount('#app');
Sign up to request clarification or add additional context in comments.

Comments

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.