Home > Enterprise >  Object is possibly 'undefined' in Vue with this.$refs
Object is possibly 'undefined' in Vue with this.$refs

Time:03-18

I switched to typescript and suddenly some of my codes are having errors with:

Object is possibly 'undefined'

The typescript version is 3.2.1

Here's the code:

this.$refs[`stud-copy-${index}`][0].innerHTML = 'Link Copied'

I've tried to add an if condition to check if it's undefined but no luck:

if (this.$refs[`stud-copy-${index}`] !== undefined) {
  this.$refs[`stud-copy-${index}`][0].innerHTML = 'Link Copied'
}

CodePudding user response:

Can you try this

(this.$refs[`stud-copy-${index}`] as HTMLElement[])[0].innerHTML = 'Link Copied'
  • Related