I just switched from plain JavaScript to Typescript (or at least I'm trying to). Here is code that worked perfectly just before, minus the typing done so far:
<script lang="ts">
import axios from 'axios';
export default {
name: "Index",
data() {
return {
years: Array,
bgs: Array
}
},
methods: {
loadYears: function (): void {
let self = this;
axios.get('../letter/get/years').then(function (response) {
function pushYears(value: any) {
self.years.push(value);
}
response.data['years'].forEach(pushYears);
});
},
loadBg: function (): void {
let self = this;
axios.get('../bg/get/all').then(function (response) {
JSON.parse(response.data['bgs']).forEach(function (obj: any) {
self.bgs.push(obj);
})
});
}
},
beforeMount() {
this.loadYears();
this.loadBg();
}
}
</script>
now after switching to Typescript, self.years.push(value) and self.bgs.push(obj) are not working anymore, the corresponding errors says: "self.$data.years.push is not a function". Interestingly enough, in the beforeMount()-function I get an error saying that loadYears() and loadBg are not defined, but accessing the data()-objects works perfectly in this block via this.data().years or bgs. How can I access these properties inside my methods-block?
bgs: Array- this is a mistake, this is not TS typing, you basically assign Array constructor, this is the reason why push is not a function.