<template>
<div>
<input ref='commandLine' />
</div>
</template>
<script>
export default {
mounted () {
window.addEventListener('focus', this.pageFocused)
},
methods: {
pageFocused: () => {
console.log('pageFocused')
console.log(this)
this.$refs.commandLine.focus()
}
}
}
</script>
I want to set focus on commandLine input every time user get into my app. Unfortunately, when I try to use $refs in order to find my <input> object, it's null.
I suspect that it's because window.addEventListerer puts my function into different context, so this variable doesn't represent my component.
What is a clean way to solve it?