0

I have run into a baffling issue with reactive properties in a Vue.js app, which probably results from a misunderstanding on my part and I hope someone can help. In my App.vue, I have import auth from 'src/auth.js', and in my data properties I have this:

 data() {
  return {
    authenticated: auth.user.authenticated,
    role: auth.user.role
  }
 },

This authenticated property is used to show or hide various menu items, e.g:

<li v-if="!authenticated">
  <router-link to="/login">Log In</router-link>
</li>
<li v-if="authenticated"><a href="#" @click.prevent="logout">Log Out</a></li>

What I'm finding is that changes to 'auth.user.authenticated' (e.g., switching from false to true after a successful login) are not being reflected in the rendering of the menu items - that is, the authenticated data property is not being updated. To do so, I have to explicitly update it in 'beforeUpdate' and the logout method, with this.authenticated=auth.user.authenticated. At first I thought it was simply that this was assigned on first creation and not subsequently updated and that if I used a computed property instead, this would be dynamic, but that doesn't do the job either. Clearly my App component is unaware of changes to the 'auth' data. How can I make things so that updating is automatic? I have it working at present but the use of 'beforeUpdate' feels like a kludge.

6
  • 2
    try making authenticated a computed property that returns auth.user.authenticated Commented Mar 10, 2017 at 21:59
  • Is auth something that is non-Vue? Commented Mar 10, 2017 at 22:00
  • thanksd, I did say above that I'd tried using a computer property. But I must have been doing something else wrong, because I just tried again in response to your comment and now I find it works! Commented Mar 10, 2017 at 22:09
  • Spoke too soon, in fact. The computed property approach does not work (I thought it did because npm run dev hadn't compiled the changed component - it sometimes does that - and I was still seeing the old version in effect). Roy J, what do you mean by 'Is auth something that is non-Vue'? Commented Mar 10, 2017 at 22:21
  • Is auth a Vue component, or some object that is not created or managed by Vue? Commented Mar 10, 2017 at 22:30

1 Answer 1

1

Vue data items are reactive, but non-Vue data items are not reactive. Your code initializes a reactive data item with the value from a non-reactive one.

There is no way for Vue to watch external (to Vue) variables for changes. You will need to notice when your variable changes and tell Vue about it, or just use the external variable (instead of an internal copy) when you need it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for clarifying my thinking on this. What I've now done is to store the credentials in a Vuex store on login and logout, and I access this store from a computed property in App. This feels a lot more Vue-y to me than what I had before.

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.