Having a small problem when accessing Vue data from within the data(), originally I was getting a few errors saying that variables were undefined but I realised pretty quickly that it was because of the order I defined them in, so I changed the order so that it wouldn't be a problem anymore but I'm still getting a cannot read property 'undefined' of undefined when trying to access an array of objects defined above whilst using a Vue variable as the index.
App.vue
export default {
data(){
return {
correct: 0,
total: 0,
currentText: 0,
position: 0,
userText: '',
texts: [{
text: 'the quick brown fox jumped over the lazy dog',
textSize: '50px'
},{
text: 'sphinx of black quartz judge my vow',
textSize: '50px'
}
],
textSize: this.texts[this.currentText].textSize
}
}
thisis not what you think it is. You cannot refer to an "under construction" object from inside the object initializer. You'll have to set up thetextSizeproperty outside the initializer in a separate statement. Or it may be thattextSizeshould really be method, so that it can dynamically return the size of the current text.this.texts[this.currentTexts].textSizecircumvent the error?textSize()were a method, then when called (if called properly, as a method of the returned object) then it would be able to usethis.thispoints to a different object. you can try putting it in acomputedhook insteadgetTextSize(){ return this.texts[this.currentText].textSize }as a vue method isn't working.