Answer the question
In order to leave comments, you need to log in
Why doesn't TypeScript allow DOM element methods for Vue Refs?
The error is actually in the method of the Vue component:
methods: {
...
dropdownTrigger (e: Event | undefined) {
if (e) {
e?.stopPropagation()
this.$refs.list.scrollTo(0, 0)
this.isListVisible = !this.isListVisible
if (!this.isListVisible) this.$refs.input.blur()
this.selected = this.value
? [...this.value]
: []
}
},
...
}
this.$refs.list.scrollTo(0, 0)
:Property 'scrollTo' does not exist on type 'Vue | Element | Vue[] | Element[]'.
Property 'scrollTo' does not exist on type 'Vue'.ts(2339)
declare module '*.vue' {
import Vue from 'vue'
export default Vue
}
import Vue, { VNode } from 'vue';
declare global {
namespace JSX {
interface Element extends VNode {}
interface ElementClass extends Vue {}
interface IntrinsicElements {
[element: string]: any;
}
}
}
Answer the question
In order to leave comments, you need to log in
Property 'scrollTo' does not exist on type 'Vue | element | vue[] | Element[]'
Vue | Element | Vue[] | Element[]
(this.$refs.list as HTMLDivElement).scrollTo(0, 0)
Also a great solution in addition to Alexey Yarkov 's answer
import Vue, { VueConstructor, VNode} from 'vue'
interface Refs {
$refs: {
list: HTMLElement,
input: HTMLInputElement,
}
}
export default (Vue as VueConstructor<Vue & Refs>).extend({
...
methods: {
dropdownTrigger (e: Event | undefined) {
this.$refs.list.scrollTo(0, 0) // наконец то отлично отрабатывает
}
}
...
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question