I wish to hold onto a reference of a jQuery element in the vue 'data' object but TypeScript is complaining at me that the type does not match.
How can I fix this?
[EDIT] Think I largely fixed this by fooling TS by referencing a non-existing jQuery element as a default value to the property. After that I can reference the prop and it believes it is a jQuery element... - it still feels a little 'hacky' though...
Any help much approciated!
var x = Vue.extend({
data: function () {
return {
_componenentDidMount: false,
_myJQueryElement: undefined,
}
},
mounted: function () {
// remember we are now mounted
this._componenentDidMount = true; // this is OK
// on mounting I wish to set the '_myJQueryElement'
this._myJQueryElement = $('.z-vertical-scrollbar'); // this FAILS
},
methods: {
B() {
// I can do this... (typescript cleverly infers the type to be 'boolean')
var didMount = this._componenentDidMount; // this is OK
// Now I wish to do 'jquery' stuff with the element
var top = this._myJQueryElement.offset().top; // this FAILS
}
}
}
(below is what the code looks like on the screen)
[EDIT 2] (the 'fixed' version - making TypeScript believe it is a jQuery element by referencing a non existing element)...
[EDIT 3] (yet another differently 'fixed' version - using TypeScript to define the properties properly).. - even though I am using 'any' as a get-out clause since jQuery returns a JQuery | undefined back which is tricky to handle...