Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js refs are undefined, even though this.$refs shows theyre there

Tags:

I have a reference to a component

 <Gmap ref="mapRef"> 

In mounted I am doing this, just to see the objects are available

 mounted(){    let self = this    console.log(self.$refs) // Shows the mapRef object reference    console.log(self.$refs.mapRef) // returns undefined ???  } 

self.$refs shows...

  mapRef: VueComponent {_uid: 207, _isVue: true, $options: {…}, _renderProxy: Proxy, _self: VueComponent, …} 

So then why does self.$refs.mapRef return undefined?? Even though its clearly there??

like image 364
Kylie Avatar asked Jan 24 '19 21:01

Kylie


People also ask

Why is ref undefined Vue?

$refs is undefined when the element with the ref attribute is not visable at the moment you use the this. $refs object. This costs me some time to figure out. It has a button that opens a div that contains an input field with a ref attribute.

Why is ref undefined?

If we try to access the current property of the ref directly in the component, we would get undefined back because the ref hasn't been set up and the div element has not been rendered. You can also access the current property of the ref in an event handler function.

Why is $refs empty Vue?

This might not be an answer to your particular problem but I discovered that another reason for $refs being empty is that the component on which it is defined has not created/mounted yet. Probably due to the lazy rendering Vue uses.

How do you use refs at Vue?

Referencing DOM Elements in Vue # Vue can store references to DOM elements in a property called $ref . The first thing we have to do, is add a ref attribute to the element we want to reference in our Javascript. The value of the ref attribute will be the name of it within the $ref property.


2 Answers

I solved this by using v-show instead of v-if.

I had the component inside a v-if statement.

 <div v-if="!isLoading">     <GMap ref="mapRef" />  </div> 

I just changed that to v-show

<div v-show="!isLoading">     <GMap ref="mapRef" />  </div> 

And now the object is available in mounted(). Still find it strange that the console.log(this.$refs) showed it being available as a key on this.$refs, even though it actually wasn't? Thats strange behaviour.

The other wierd thing was, that even if I tried to access this.$refs.mapRef in my data loading section, AFTER THE DATA WAS LOADED, (ie after isLoading=false), I still couldn't access it. Even though then, it should've been available because the v-if passed.

So v-show solved it, by just hiding the div, instead of not rendereding it. Stupid little workaround.

like image 137
Kylie Avatar answered Nov 17 '22 04:11

Kylie


I had a similar problem getting a ref to a leaflet map instance, try waiting for the "nextTick"

mounted(){   this.$nextTick(()=>{     let self = this     console.log(self.$refs) // Shows the mapRef object reference     console.log(self.$refs.mapRef) // returns undefined ???   }); } 

see the docs for more- https://vuejs.org/v2/api/#vm-nextTick and https://vuejs.org/v2/api/#mounted

like image 35
chrismarx Avatar answered Nov 17 '22 05:11

chrismarx