javascript - Property '$el' does not exist on type 'never' - Stack Overflow
I am working on a vue.js 3
project, where I need to refer/get a div
element in my script by initially declaring a null
reference like this const refRouterView = ref(null)
and I am not sure how to make TypeScript aware of DomElement
.
Markup updated
<template lang="pug">
.flex
router-view(ref='refRouterView')
...
</template>
<script lang="ts" setup>
import { ref, onMounted } from 'vue';
const refRouterView = ref(null);
const routerViewHeight = ref(500);
const getRouterViewHeight = () => {
setTimeout(() => {
routerViewHeight.value = refRouterView.value?.$el.offsetHeight;
}, 250);
};
onMounted(() => getRouterViewHeight());
</script>
I was getting this error Object is possibly 'null'
before adding ?
to refRouterView.value?
which seems like hack and I don't like it. And, after adding ?
I started seeing this error Property '$el' does not exist on type 'never'.
I tried adding HTMLElement
to const refRouterView : HTMLElement = ref(null);
but that introduce this error Property 'value' does not exist on type 'HTMLElement'.
which was not solved even after adding ?
to refRouterView.value?
Please help!
More Info
Originally my question was using div(ref='refRouterView')
and @Thomas's answer solves it but I was using router-view
. Just for making my question easy to understanding I mentioned div
instead of router-view
and that confused us, hence the answer did not solve my issue.
<template lang="pug">
.flex
div(ref='refRouterView')
...
</template>
I am working on a vue.js 3
project, where I need to refer/get a div
element in my script by initially declaring a null
reference like this const refRouterView = ref(null)
and I am not sure how to make TypeScript aware of DomElement
.
Markup updated
<template lang="pug">
.flex
router-view(ref='refRouterView')
...
</template>
<script lang="ts" setup>
import { ref, onMounted } from 'vue';
const refRouterView = ref(null);
const routerViewHeight = ref(500);
const getRouterViewHeight = () => {
setTimeout(() => {
routerViewHeight.value = refRouterView.value?.$el.offsetHeight;
}, 250);
};
onMounted(() => getRouterViewHeight());
</script>
I was getting this error Object is possibly 'null'
before adding ?
to refRouterView.value?
which seems like hack and I don't like it. And, after adding ?
I started seeing this error Property '$el' does not exist on type 'never'.
I tried adding HTMLElement
to const refRouterView : HTMLElement = ref(null);
but that introduce this error Property 'value' does not exist on type 'HTMLElement'.
which was not solved even after adding ?
to refRouterView.value?
Please help!
More Info
Originally my question was using div(ref='refRouterView')
and @Thomas's answer solves it but I was using router-view
. Just for making my question easy to understanding I mentioned div
instead of router-view
and that confused us, hence the answer did not solve my issue.
<template lang="pug">
.flex
div(ref='refRouterView')
...
</template>
Share
Improve this question
edited Nov 19, 2021 at 14:47
Syed
asked Nov 19, 2021 at 8:18
SyedSyed
16.6k15 gold badges127 silver badges157 bronze badges
4 Answers
Reset to default 3If you ref
a DOM node, you don't have an $el
property, the ref directly points to the DOM node:
https://v3.vuejs/guide/position-api-template-refs.html
And for the typescript part, you have to define the type like this as the refRouterView
is a Proxy
of a certain type:
const refRouterView = ref<HTMLDivElement>();
Then you can directly access the attributes:
refRouterView.value.offsetHeight
(this.$refs.refRouterView as Vue & { $el: () => any }).$el.offsetHeight
Please refer to it: Vetur bug or code error? “property ‘$el’ does not exist on type ‘SVGelement’
const refRouterView = ref<typeof RouterView | null>(null)
If you ref is an origin Element, then you don't have to access $el
, you can access the dom directily by .value
. You need to access $el
while you are trying to access a custom ponent, and you need to get the type with ref<InstanceType<typeof YourComponent> | null>(null)
, here below is an example:
<template>
<div class="canvas" ref="canvas"></div>
<knife_map ref="svgContainer" class="knife_map" :style="{ transform: svgPosition }"></knife_map>
</template>
<script setup lang="ts">
import knife_map from './ponents/knife_map.vue';
const canvas = ref<HTMLDivElement | null>(null)
onMounted(() => {
const { width, height }: { [x: string]: number } = svgContainer.value?.$el.getBoundingClientRect()
const { width: cvs_width, height: cvs_height } = canvas.value?.getBoundingClientRect() || {}
console.log('width,height', width, height)
console.log('cvs_width,cvs_height', cvs_width, cvs_height)
})
</script>
- Windows 10硬件产品发布会汇总 微软喊你换电脑
- 微软推绿色IT挑战新网站促进IT节能环保
- 鲍尔默:未来5至10年微软将不再像一家软件公司
- 应用软件战场:安卓不敌苹果
- Pre-increment and Post-increment in C - Stack Overflow
- How to remove internal padding to the right of SwiftUI Picker - Stack Overflow
- python - How to Get a Single Solution from a Physics-Informed Neural Network (PINN) for a PDE with Initial and Boundary Conditio
- apache spark - Can't save pyspark ML model :py4j.protocol.Py4JJavaError: An error occurred while calling o577.save. : ja
- perl - How to embed Teraterm in a Visual Studio project - Stack Overflow
- AWS CloudFront occasionally does not send request to custom origin server - Stack Overflow
- asp.net - Razor Pages 'asp-page-handler' did not working [POST] - Stack Overflow
- Teradata: How can I trim a column for leading zeros and trailing spaces? - Stack Overflow
- database - Issue in call command during switchover mariadb - Stack Overflow
- Azure monitor alert using minimum failing periods for static threshold - Stack Overflow
- testrigor - Unable to capture values from card display - Stack Overflow
- How to I get excel to look up values in the nth column based on a number in a cell? - Stack Overflow
- next.js - Update Server Component via a Client Component - Stack Overflow