question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The $refs maybe undefined

See original GitHub issue

Version

2.6.10

Reproduction link

https://codesandbox.io/embed/3q34mqqwxm

Steps to reproduce

  1. In the template not ref use.
  2. In the ts use readonly $refs!: { div: Element };
  3. 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
juliussacommented, Oct 17, 2019

In my case.

<template>
<div>
    ...
    <application-gateway
      v-if="data && vccCredit"
      :flowData="data"
      :vccCredit="vccCredit"
      :vccPartners="vccPartners"
      ref="applicationGateway"
    >
    </application-gateway>
    ...
</div>
</template>

<script>
  async applyNowHandler() {
    const applicationGateway = this.$refs.applicationGateway as any
    // prevent amazing Cannot read property 'start' of undefined
    this.$nextTick(() => {
      applicationGateway.start()
    })
  }
</script>

I promise add loading when fetching data and vccCredit. And add the $nextTick.

But applicationGateway ref still be undefined in some case.

image

I don’t know why?

May be we need change the $ref define in types/vue.d.ts.

https://cn.vuejs.org/v2/api/#ref

@underfin , support you.

Do you try to use v-show instead v-if?

1reaction
LinusBorgcommented, Apr 15, 2019

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found