I'm trying to get the value of a props inside of a component, but the returned value is incorrect.
App.vue
<template>
<v-app>
<router-view :authentication="auth"></router-view>
</template>
<script>
import auth from '../auth.js';
export default {
data : () => ({
auth : auth
}),
mounted: function () {
this.$nextTick(function () {
auth.check()
})
}
}
</script>
Dashboard.vue (gets rendered as the router-view)
<template>
<h1>You're Logged In!</h1>
</template>
<script>
export default {
props: ['authentication'],
mounted: function() {
this.$nextTick(function () {
console.log(this.authentication.user.authenticated) // Returns false (should be true)
}
)
}
}
</script>
auth.js
export default {
user: {
authenticated: false,
profile: null
},
check() {
let token = localStorage.getItem('id_token')
if (token !== null) {
Vue.http.get(
'api/user?token=' + token,
).then(response => {
this.user.authenticated = true
this.user.profile = response.data.user
})
}
}
}
The result of console.log(this.authentication.user.authenticated)
in Dashboard.vue is returned as false
, but the Vue devtools indicated that the value should be true
(which is what should be returned).
Here is a screenshot of the props in my Dasboard.vue
component through the Vue devtools:
What I've Tried
1) I've tried changing the value of the user
object in auth.js
from false
to true
and THEN I get the correct output in Dashboard.vue
. However, this is no good since the check()
function is what should be updating the value of the user
object in auth.js
.
2) I've logged the result of this.authentication
in Dashboard.vue
and I can clearly see that the nested object user.authenticated
is true, yet the result of console.log(this.authentication.user.authenticated)
is still false for some reason.
Here is a screenshot of the logged result of console.log(this.authentication)
:
mounted
lifecycle handler ofDashboard.vue
is firing beforeauthenticated
is set to true.nextTick
isn't going to fix that, the Vue lifecycle is on the order of 15ms and the ajax call is likely taking much longer than that. – Bertmounted
lifecycle handler to read the most up-to-date version of theauthentication
prop inDashboard.vue
– Robert Cooper