Using Shared Data Properties
In response to suggestions from @scar-2018 in the comments section, it is recommended to define name_student
as a data prop so that it can be accessed by all component methods and hooks:
export default {
data() {
return {
name_student: '',
}
},
methods: {
getStudent() {
axios.get(/*...*).then((result) => {
this.name_student = result.data.name
this.updateStudentName()
})
},
updateStudentName() {
this.name_student += ' (student)'
}
}
}
Handling Asynchronous Data Access
If you are encountering issues with accessing data asynchronously and seeing undefined
when logging name_student
, it may be due to the asynchronous nature of the axios
callback. Ensure that you are logging this.name_student
after it has been modified by axios
:
axios.get(/*...*/).then((result) => {
this.name_student = result.data.name
console.log(this.name_student) // => 'foo' ✅
})
console.log(this.name_student) // => undefined ❌ (not yet set)