Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Changing html element attribute using vue $refs

I am trying to access the href attribute of a link element in a distant componant.
The @click func will generate a new href and then place it in the link element.

Here is my current link:

 <a ref="svglink" href="javascript:void(0)"  @click="saveSvg2" download="testSvg.svg" >EXPORT SVG</a>


 

I try to place the new url like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

this.$refs.svglink.href = url;
```
But I get a "Property 'href' does not exist on type 'Vue'" message from my IDE and the code is not working.

What am I doing wrong?

>Solution :

Firstly, you should use ref="svglink" instead of id="svglink" if you want to use a ref.

However, there’s a better way than this by using reactive properties.

<template>
  <a id="svglink" :href="url"  @click="saveSvg2" download="testSvg.svg" >EXPORT SVG</a>
</template>

<script>
export default {
  data() {
    return {
      url: ''
    }
  },
  mounted() {
    // whenever you update this.url the link will update
    // doesn't have to be in mounted(), this is just an example
    this.url = 'http://example.com'
  }
}
</script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading