I've implemented two methods in my Vue instance;
const app = new Vue({
el: '#app',
data: {
id: null
},
methods: {
getId: function() {
return axios.get('url')
.then(response => response.data)
.then(id => this.id = id)
.catch(error => console.log(error))
},
foo: function() {
console.log(this.id)
}
},
mounted: function() {
this.getId()
this.foo()
}
})
The console.log()
displays null
because it executes before the response from getId()
has a chance to set the id
value. This can be observed using the Vue developer tools, where the id is correctly displayed rather than as null
.
Is there a way to ensure that getId()
sets the value before calling this.foo()
?