The $refs maybe undefined
See original GitHub issueVersion
2.6.10
Reproduction link
https://codesandbox.io/embed/3q34mqqwxm
Steps to reproduce
- In the template not ref use.
- In the ts use
readonly $refs!: { div: Element };
- In the runtime
this.div.xx
will throw error.
What is expected?
Throw error in compiler-time.
What is actually happening?
Throw error in runtime.
we can use readonly $refs!: { div: Element | undefined };
and this.div!.xx
.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:13 (8 by maintainers)
Top Results From Across the Web
Ref object is possibly undefined TypeScript React
I want to add a class when the input value is more than 0 characters in length, however I'm coming across an issue...
Read more >[SOLVED] This.$refs.key returns undefined when it really is
I am using Vue 2.0 with Webpack. When I say console.log(this.$refs) (inside created() method inside a low level component) it retu…
Read more >Ref object is possibly undefined TypeScript React-Reactjs
[Solved]-Ref object is possibly undefined TypeScript React-Reactjs ... And that means you have to handle null as a possible value of the current...
Read more >Documentation - TypeScript 2.0
TypeScript has two special types, Null and Undefined, that have the values null and undefined respectively. ... x; // Error, x is possibly...
Read more >Understanding the exclamation mark in TypeScript
For example, if we define a variable as possibly a string or undefined, the ! operator tells the compiler to ignore the possibility...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Do you try to use v-show instead v-if?
@underfin I fail to see what you think wrong about the behaviour you are showing. There’s no element to reference, so $refs is empty.
It seems you think you should be able to overwrite $refs with a permanent value. That’s not the case. $refs is created dynamically during runtime, and will always only contain the refs that are currently found in the document.
@HyunmoAhn Your problem seems unrelated. The
<!-- -->
comment in the page’s markup indicates that you have a render error, which is why there’s no ref: Because of the render error, no element was created, so there’s no element for the ref to reference.